2013-07-10 62 views
0

我目前使用Sentiment140 API對一些推文進行分類。我完全用Python編寫。 一切都很好,但我有美化輸出和存儲它的問題。保存來自Sentiment140的JSON輸出API

我用下面的代碼來存儲檢索的數據:

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) 

print語句給了我下面的:

{"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"}}]} 

這意味着所有我需要的數據。不幸的是在一行,但確定。 現在我嘗試將數據保存在適當和更漂亮的格式中。保存的文件如下所示:

"{\"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" 

仍然全部位於同一行,並與所有「\」。我錯過了哪個命令?

回答

0

我自己找到答案。雖然我不知道它是否是最美的方式:

# 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) 

沒有工作。