Tom
Tom

Reputation: 435

Transform JSON into CSV with Python issues

I am trying to convert a JSON file selectively into a CSV. Meaning that I want to iterate over the JSON.

The code to write my CSV looks like this:

f = csv.writer(open("test.csv", "wb+"))
f.writerow(["id", "text", "polarity"])
for x in final:
    f.writerow([x["id"], 
                x["text"], 
                x["polarity"]])

Sadly, I get the following error:

TypeError: string indices must be integers

I have already an idea what is the problem. I checked the type of my JSON after loading it. It is a dict so that should be fine.

When I print my dict:

print (final)

I get:

{u'data': [{u'polarity': 2, u'text': u'How deep is your love - Micheal Buble Ft Kelly Rowland ?', u'meta': {u'language': u'en'}, u'id': u'1'}, {u'polarity': 2, u'text': u'RT @TrueTeenQuotes: #SongsThatNeverGetOld Nelly ft. Kelly Rowland - Dilemma', u'meta': {u'language': u'en'}, u'id': u'2'}, {u'polarity': 2, u'text': u'RT @GOforCARL: Dilemma - Nelly Feat. Kelly Rowland #Ohh #SongsThatNeverGetOld', u'meta': {u'language': u'en'}, u'id': u'3'}, {u'polarity': 2, u'text': u'#NP Kelly Rowland Grown Woman', u'meta': {u'language': u'en'}, u'id': u'4'}, {u'polarity': 2, u'text': u"My friend just said 'kelly rowland is gettin it good... Most of her songs are sexual'", u'meta': {u'language': u'en'}, u'id': u'5'}, {u'polarity': 2, u'text': u'No. Kelly Rowland is the Black Barbie, idc', u'meta': {u'language': u'en'}, u'id': u'6'}, {u'polarity': 2, u'text': u'Nelly - Gone ft. Kelly Rowland http://t.co/tXjhCS05l0', u'meta': {u'language': u'en'}, u'id': u'7'}, {u'polarity': 2, u'text': u'Kisses Down Low by Kelly Rowland killer?\u2018?\u2018?\u2018 #NellyVille', u'meta': {u'language': u'en'}, u'id': u'8'}]}

Where every item seems to be in Unicode besides the value for 'polarity'. I have now 3 questions. 1. Should all items be in unicode or not? How can I change the format within the dict? Will that solve my problem?

Upvotes: 1

Views: 214

Answers (1)

jsbueno
jsbueno

Reputation: 110301

Iterating over a dictionary in Python gives you the dict's keys as strings - thus x in your example above will contain just the string "data"

If you want to iterate over the dicts in the list value associated to the u"data" key of your final dict, you have to write that in Python:

...
for x in final[u"data"]:
    f.writerow([x["id"], 
                x["text"], 
                x["polarity"]])

Upvotes: 1

Related Questions