JSON转换脚本中的“Unicode”对象错误

2024-04-26 13:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从一个教程(在http://stats.seandolinar.com/collecting-twitter-data-converting-twitter-json-to-csv-ascii/)开始工作,将集合内容从json文件复制到csv文件。在

我一直收到属性命名错误“AttributeError:'unicode'对象没有属性'get'”

这是我目前一直在尝试的代码。如有任何建议,将不胜感激。在

json_src = open('data/{}.json'.format(search_hash), mode='r').read()
json_strng = json.loads(json_src)

csv_file = open('data/{}.csv'.format(search_hash), mode='w')

write_obj = csv.writer(csv_file)

fields = ['created_at', 'text', 'screen_name', 'location',
          'y_coord', 'x_coord']
print "CSV Headers created"
write_obj.writerow(fields)


for line in json_strng:

    write_obj.writerow([line.get('created_at'),
                        line.get('text'),
                        line.get('user').get('screen_name'),
                        line.get('user').get('location'),
                        line.get('geo').get('coordinates').get('0'),
                        line.get('geo').get('coordinates').get('1')])

csv_file.close()
print "CSV File Completed"

Tags: 文件csvsrcjsonobjformatdataget