我试图编写一个包含Unicode到JSON的pandas数据帧,但是内置的.to_json函数会忽略字符。我该怎么解决这个问题?
例子:
import pandas as pd
df = pd.DataFrame([['τ', 'a', 1], ['π', 'b', 2]])
df.to_json('df.json')
这就产生了:
{"0":{"0":"\u03c4","1":"\u03c0"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
与预期结果不同:
{"0":{"0":"τ","1":"π"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}
我尝试添加force_ascii=False参数:
import pandas as pd
df = pd.DataFrame([['τ', 'a', 1], ['π', 'b', 2]])
df.to_json('df.json', force_ascii=False)
但这会产生以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\u03c4' in position 11: character maps to <undefined>
最佳答案
打开一个编码设置为utf-8的文件,然后将该文件传递给.to_json函数,可以解决此问题:
with open('df.json', 'w', encoding='utf-8') as file:
df.to_json(file, force_ascii=False)
给出正确的:
{"0":{"0":"τ","1":"π"},"1":{"0":"a","1":"b"},"2":{"0":1,"1":2}}