Reputation: 847
I am a beginner in python and I am trying to parse information from eml files in python. I set up my extract
function to parse what I want to get. The problem is I have 10,000+ files and I don't know how to add every extracted information to one object.
When I run the extract
function on one eml file, the output looks like
{'from': 'Joe', 'to': 'Robert', 'text': 'Hey Robert'}
The output is a dict
object. Now I want to append my extracted ouput to my existing output (name
) to collect every information from 10,000 files in name
. How can I do this? I used following code, but the name
object only has the information from last file in FList
(which is a list of each 10,000+ files).
for i in range(len(FList)):
f = open(FList[i])
name=extract(f, f.name)
f.close()
Upvotes: 0
Views: 91
Reputation: 46759
It all depends on how you want to store your data. If you just want a list of entries, then you just need to append the extracted data to a list as follows:
name = []
for file_name in FList:
with open(file_name) as f:
name.append(extract(f, f.name))
In extract()
, you could add another dictionary entry to hold the filename of the entry before returning it.
Upvotes: 0
Reputation: 3056
The name
object is being overwritten in your loop. Since name is meant to be a dict, and dicts are passed by reference (google on pass by reference to get more info), you can do something like this:
names = dict()
for my_file in file_lst:
with open(my_file) as f:
extract(f,names)
def extract(f, names):
#modify your names dict here such as:
names["something new"] = "a new value"
After you are done with looping over file_lst
you will have names populated with all the info from all files, as it will be retained across the loop...
Upvotes: 0
Reputation: 309881
It looks like you want to map the filename to the dictionary with the data from the file. To do that, you'd do something like:
file_map = {}
for fname in FList:
with open(fname) as f:
file_map[fname] = extract(f, fname)
Upvotes: 1