with open(filename+'.json') as json_file:
data=pd.io.json.read_json(json_file,encoding='utf_16_be')我尝试了多种编码选项,但都失败了。它返回空对象。只有在没有物料清单的情况下将文件保存为UTF8格式时,我才能进行转换。我像平常一样用默认编码打开它:
with open(filename+'.json') as json_file:
data=pd.io.json.read_json(json_file)文件的默认编码为UTC-2 Little Endian。如何使用此编码读取json?
发布于 2021-10-12 12:29:21
阅读并关注import pandas as pd; help (pd.io.json.read_json)。以下(部分注释的)代码片段可能会有所帮助:
filename = r"D:\PShell\DataFiles\61571258" # my test case
import pandas as pd
filepath = filename + ".json"
# define encoding while opening a file
with open(filepath, encoding='utf-16') as f:
data = pd.io.json.read_json(f)
# or open file in binary mode and decode while converting to pandas object
with open(filepath, mode='rb') as f:
atad = pd.io.json.read_json(f, encoding='utf-16')
# ensure that both above methods are equivalent
print((data == atad).values)输出:.\SO\69537408.py
[ True]
https://stackoverflow.com/questions/69537408
复制相似问题