일부 JSON Twitter 데이터를 수집하고 CSV 파일의 개별 열로 일부 특정 필드를 출력하는 간단한 코드가 있습니다. 내 문제는 내가 UTF-8로 출력을 인코딩하는 적절한 방법을 찾아 낼 수 없다는 것이다. 아래는 회원의 도움을 받아 내가 가졌던 가장 가까운 것입니다.하지만 여전히 여전히 올바르게 실행되지 않고 트윗 텍스트 필드의 고유 한 문자 때문에 실패합니다. 문서에서CSV에 쓸 때 UTF-8 인코딩하기
import json
import sys
import csv
import codecs
def main():
writer = csv.writer(codecs.getwriter("utf-8")(sys.stdout), delimiter="\t")
for line in sys.stdin:
line = line.strip()
data = []
try:
data.append(json.loads(line))
except ValueError as detail:
continue
for tweet in data:
## deletes any rate limited data
if tweet.has_key('limit'):
pass
else:
writer.writerow([
tweet['id_str'],
tweet['user']['screen_name'],
tweet['text']
])
if __name__ == '__main__':
main()
감사합니다. @ user2100799 -'.encode ('utf-8')의 모든 변형을 시도해 보았습니다. 설명서를 읽었지만 여전히 제대로 작동하지 않습니다. CSV 모듈. 다른 제안? –
여기에서 시도하십시오. http://stackoverflow.com/questions/5838605/python-dictwriter-writing-utf-8-encoded-csv-files – 1478963