用Unicode将Pandas DataFrame写入JSON [英] Writing pandas DataFrame to JSON in unicode
本文介绍了用Unicode将Pandas DataFrame写入JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将包含unicode的pandas DataFrame写入json,但是内置的.to_json
函数转义了字符.我该如何解决?
I'm trying to write a pandas DataFrame containing unicode to json, but the built in .to_json
function escapes the characters. How do I fix this?
示例:
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
参数:
I have tried adding the
force_ascii=False
argument:
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>
我正在将WinPython 3.4.4.2 64bit与pandas 0.18.0一起使用
I'm using WinPython 3.4.4.2 64bit with pandas 0.18.0
推荐答案
打开编码设置为utf-8的文件,然后将该文件传递给.to_json
函数可解决此问题:
Opening a file with the encoding set to utf-8, and then passing that file to the .to_json
function fixes the problem:
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}}
注意:它仍然需要force_ascii=False
参数.
Note: it does still require the force_ascii=False
argument.
这篇关于用Unicode将Pandas DataFrame写入JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文