简体   繁体   中英

How to add dictionary items when I loop in python

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()

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)

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM