Anonymous Anonymous - 15 days ago 8
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 = api.search( 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'))
dict_writer.writeheader()
dict_writer.writerows(dic)


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

dict_writer.writerow(dic)


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

Any thoughts?

Answer

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.

dict_writer.writerows([dic])

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']
    dictlist.append(dic)

dict_writer.writerows(dictlist)