我正在处理感受分析,在用twython获取twitter数据并将它们保存在json格式的txt文件中后,我需要以CSV格式编写它们。我可以这样做,但不写特殊字符,例如“Inclusão”写成“Inclus \ xc3 \ xa3o” 这是代码:
import json
from csv import writer
with open('data.txt') as data_file:
data = json.load(data_file)
tweets = data['statuses']
#variables
times = [tweet['created_at'] for tweet in tweets]
users = [tweet['user']['name'] for tweet in tweets]
texts = [tweet['text'] for tweet in tweets]
#output file
out = open('tweets_file.csv', 'w')
print(out, 'created,user,text')
rows = zip(times,users,texts)
csv = writer(out)
for row in rows:
values = [value.encode('utf8') for value in row]
csv.writerow(values)
out.close()
答案 0 :(得分:0)