简体   繁体   English

使用Python中的UTF8编码从JSON转换为CSV

[英]JSON to CSV with UTF8 encoding in Python

I'm working with analysis of feelings and after having got twitter data with twython and saving them in a txt file in json format, I need to write them in CSV format. 我正在分析感觉,并在使用twython获得Twitter数据并将其保存为json格式的txt文件后,我需要以CSV格式编写它们。 I can do this but special characters are not written, for example "Inclusão" is written "Inclus \\ xc3 \\ xa3o" here is the code: 我可以这样做,但是没有写特殊字符,例如,“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()

i already solved the problem guys, thank you! 我已经解决了这个问题,谢谢! the problem is that my text has already been encoded and i was trying to do this again. 问题是我的文本已经被编码了,我正尝试再次这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM