我是 Python 的初学者,我一直在尝试将我的数据导出到 csv 文件,但我不知道如何摆脱所有括号和逗号分隔。理想情况下,我需要两列:一列包含“count”的所有值,另一列包含“month”的值。
任何建议表示赞赏。
我的代码:
from sunlight import capitolwords
import requests
import csv
r = requests.get ('http://capitolwords.org/api/1/dates.json?phrase=guns&start_date=2011-
12-01&end_date=2013-01-
15&granularity=month&sort=count&apikey=ab02633fb17841d09f4c3660e0384ae5')
data = r.text
ifile = open('guns.csv', 'rb')
reader = csv.reader(data.splitlines(), delimiter=',')
for row in reader:
print row
结果:
['{']
[' "results": [']
[' {']
[' "count": 62.0', '']
[' "month": "201212"']
[' }', '']
[' {']
[' "count": 36.0', '']
[' "month": "201207"']
[' }', '']
[' {']
[' "count": 35.0', '']
[' "month": "201112"']
[' }', '']
[' {']
[' "count": 27.0', '']
[' "month": "201202"']
[' }', '']
[' {']
[' "count": 27.0', '']