2
\$\begingroup\$

I have a defaultdict being written to file like this:

writer = csv.writer(open(locationToSaveFile, 'wb+'))    
for k,v in dict.iteritems():
    writer.writerow([k ,v])

I then have this horribly convoluted text to read it back in:

myDict = defaultdict(list) 
with open(fileLocation, 'rb') as test:
    reader = csv.DictReader(test, ['processedText', 'date'])

    for line in reader:
        myDict[line['processedText']].append(line['date'])

textDict = defaultdict(list) 

for processedText, date in myDict.items():
    actual_list = ast.literal_eval(date[0])
    for a in actual_list:
        textDict[processedText].append(a)

for processedText, date in textDict.items(): 

Obviously this looks very wasteful (and looks horrible!) but I don't know how else to import the defaultdict.

When opening it with the reader = and for line in reader lines it creates a list within a list. The only way out of this is to use ast to convert it.

Could somebody advise me how to tidy this up?

Note: I'm very unfamiliar with Python

\$\endgroup\$
4
\$\begingroup\$

csv is meant to store rows of data, not a simple dictionary. You can use json for that:

import json

# writing
json.dump(yourdict, open(filename, 'w'))

# reading
yourdict = json.load(open(filename))
\$\endgroup\$
  • \$\begingroup\$ How do I read it back from JSON. I can write it using json.dump(filelocation, defaultdict). But how do I import it again? \$\endgroup\$ – Andrew Martin Sep 3 '13 at 16:56
  • \$\begingroup\$ Andrew, use json.load(filelike_object) \$\endgroup\$ – Mischa Arefiev Sep 8 '13 at 12:19
0
\$\begingroup\$

Your dict's values are lists. A possible approach is to expand those lists into csv columns, producing a file where the number of columns varies from row to row:

#writing
with open(locationToSaveFile, 'w') as f:
    writer = csv.writer(f)    
    for k,v in mydict.iteritems():
        writer.writerow([k] + v)

#reading
with open(locationToSaveFile, 'r') as f:
    reader = csv.reader(f)    
    mydict = collections.defaultdict(list)
    for row in reader:
        mydict[row[0]] = row[1:]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.