首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Pandas读取以UCS-2 Little Endian编码的文件的JSON

无法使用Pandas读取以UCS-2 Little Endian编码的文件的JSON
EN

Stack Overflow用户
提问于 2021-10-12 08:36:20
回答 1查看 22关注 0票数 0
代码语言:javascript
复制
with open(filename+'.json') as json_file:
data=pd.io.json.read_json(json_file,encoding='utf_16_be')

我尝试了多种编码选项,但都失败了。它返回空对象。只有在没有物料清单的情况下将文件保存为UTF8格式时,我才能进行转换。我像平常一样用默认编码打开它:

代码语言:javascript
复制
with open(filename+'.json') as json_file:
data=pd.io.json.read_json(json_file)

文件的默认编码为UTC-2 Little Endian。如何使用此编码读取json?

EN

回答 1

Stack Overflow用户

发布于 2021-10-12 12:29:21

阅读并关注import pandas as pd; help (pd.io.json.read_json)。以下(部分注释的)代码片段可能会有所帮助:

代码语言:javascript
复制
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]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69537408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档