Anonymous Anonymous - 1 year ago 173
Python Question

Dictionary to CSV - Python

I am attempting to print a dictionary to CSV and receive this error when I run the code.

Error: ValueError: dict contains fields not in fieldnames: 'u', 'r', 'l'

from nytimesarticle import articleAPI
import csv
api = articleAPI('API Key')

res = q = ['Abbott Laboratories'],
fq = {'source':['The New York Times']},
begin_date = 20110101, end_date = 20131231,
facet_field = ['source'], facet_filter = True)

for m in res['response']['docs']:
dic = {}
dic['url'] = m['web_url']
dic['id'] = m['_id']
print dic

with open('Abbot_test.csv', 'wb') as output_file:
dict_writer = csv.DictWriter(output_file, ('url', 'id'))

When I run with

dict_writer = csv.DictWriter(outputfile, ('url', 'id', 'u', 'r', 'l')

Error: AttributeError: 'str' object has no attribute 'get'

As a side note, when I run the block of code above with


In the last line, it prints a single url to the CSV with no error.

Any thoughts?

Answer Source

Well you're trying to write multiple rows, with the writerows method, but you only supply 1 row. You need to supply a list, of dicts, that contain the proper keys. When Python tries to split the list into individual objects when it loops, it fails because it tries to split one object. This is also why writerow (no s) works for you, because you supply only one dict.

In short, supply a list of dictionaries, not one.


But seems like you're only creating one and rewriting it - you probably want to write it into a list instead.

dictlist = []
for m in res['response']['docs']:
    dic = {}
    dic['url'] = m['web_url']
    dic['id'] = m['_id']

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download