将JSON数据写入python中的.csv文件时出现Unicode编码错误

时间:2018-08-16 14:10:04

标签: python json csv file-handling

我想将json数据写入.csv文件,但出现此错误

  

UnicodeEncodeError:'ascii'编解码器无法在位置9编码字符u'\ U0001f61c':序数不在范围内(128)

这是我的代码

import os
import json
import facebook
import csv
import pandas as pd
from pandas.io.json import json_normalize

output_file = open("posts.csv", "w")
writer = csv.writer(output_file)

if __name__ == "__main__":
token = os.environ.get("FACEBOOK_TEMP_TOKEN")
graph = facebook.GraphAPI(access_token=token)
profile = graph.get_object('me/posts',fields = 'message, description')
#[x['id'] for x in profile['friends']['data']]
json_data = json.dumps(profile, indent=4)
json_data_ = json.loads(json_data)
for distro in json_data_["data"]:
    writer.writerow(distro.values())

0 个答案:

没有答案