I am currently using the Sentiment140 API to classify some tweets. I am writing completely in Python. Everything is going fine but I have issues with prettifying the output and storing it.
I use the following code to store the retrieved data:
data = json.dumps(values) # instead of urllib.urlencode(values)
response = urllib2.urlopen(url, data)
page = response.read()
print page
with open('result.json', 'w') as f:
json.dump(page, f, indent=2)
The print statement gives me the following:
{"data":[{"id":"1","text":"How deep is your love - Micheal Buble Ft Kelly Rowland \\u00e2\\u2122\\u00a5","polarity":2,"meta":{"language":"en"}},{"id":"2","text":"RT @TrueTeenQuotes: #SongsThatNeverGetOld Nelly ft. Kelly Rowland - Dilemma","polarity":2,"meta":{"language":"en"}},{"id":"3","text":"RT @GOforCARL: Dilemma - Nelly Feat. Kelly Rowland #Ohh #SongsThatNeverGetOld","polarity":2,"meta":{"language":"en"}},{"id":"4","text":"#NP Kelly Rowland Grown Woman","polarity":2,"meta":{"language":"en"}}]}
Which means all the data that I need... sadly in one row but OK. Now I try to save the data in an adequate and prettier format. The saved file looks like that:
"{\"data\":[{\"id\":\"1\",\"text\":\"How deep is your love - Micheal Buble Ft Kelly Rowland \\\\u00e2\\\\u2122\\\\u00a5\",\"polarity\":2,\"meta\":{\"language\":\"en\"}},{\"id\":\"2\",\"text\":\"RT @TrueTeenQuotes: #SongsThatNeverGetOld Nelly ft. Kelly Rowland - Dilemma\",\"polarity\":2,\"meta\":{\"language\":\"en\"}},{\"id\":\"3\",\"text\":\"RT @GOforCARL: Dilemma - Nelly Feat. Kelly Rowland #Ohh #SongsThatNeverGetOld\",\"polarity\":2,\"meta\":{\"language\":\"en\"}},{\"id\":\"4\",\"text\":\"#NP Kelly Rowland Grown Woman\",\"polarity\":2,\"meta\":{\"language\":\"en\"}}]}\n"
Still all in one row and with all the "\". Which command am I missing?
Found the answer myself. Although I do not know whether it is the most beautiful way:
# Encode file correctly
final = unicode(page, errors='ignore')
# Load file correctly
final = json.loads(final)
with open('result.json', 'w') as f:
json.dump(final, f, indent=0)
Did the job.