2017-08-25 79 views
0

我正在分析感受,并且使用twython获取twitter数据并将它们保存为json格式的txt文件后,我需要以CSV格式编写它们。我能做到这一点,但特殊字符不写,例如“Inclusão”写入“Inclus \ XC3 \ xa3o” 这里是代码:在Python中使用UTF8编码的JSON转换为CSV

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

'out = open('tweets_file.csv','w',encoding ='latin-1')' –

+0

它不起作用 –

回答

0

我已经解决了这个问题,大家好,谢谢!问题是我的文本已经被编码了,我正试图再次这样做。