gpt4 book ai didi

json - 预期对象或值 : Importing JSON into Python (Pandas)

转载 作者:行者123 更新时间:2023-11-30 09:43:57 24 4
gpt4 key购买 nike

我正在使用 Python 3,并且我尝试过

data = pd.read_json('file.json',encoding="utf-8",orient='records',lines=True)

但它给了我:

ValueError:期望的对象或值

这是 Json 文件的结构,只是一个快速示例

{ 
"_id" : ObjectId("5af1b1fd4f4733eacf11dba9"),
"centralPath" : "XXX2",
"viewStats" : [
{
"totalViews" : NumberInt(3642),
"totalSheets" : NumberInt(393),
"totalSchedules" : NumberInt(427),
"viewsOnSheet" : NumberInt(1949),
"viewsOnSheetWithTemplate" : NumberInt(625),
"schedulesOnSheet" : NumberInt(371),
"unclippedViews" : NumberInt(876),
"createdOn" : ISODate("2017-10-13T18:06:45.291+0000"),
"_id" : ObjectId("59e100b535eeefcc27ee0802")
},
{
"totalViews" : NumberInt(3642),
"totalSheets" : NumberInt(393),
"totalSchedules" : NumberInt(427),
"viewsOnSheet" : NumberInt(1949),
"viewsOnSheetWithTemplate" : NumberInt(625),
"schedulesOnSheet" : NumberInt(371),
"unclippedViews" : NumberInt(876),
"createdOn" : ISODate("2017-10-13T19:11:47.530+0000"),
"_id" : ObjectId("59e10ff3eb0de5740c248df2")
}
]

}

通过这种方法,我能够看到数据,但我想要

with open('file.json', 'r') as viewsmc:
data = viewsmc.readlines()

这样输出

['{ \n',
' "_id" : ObjectId("5af1b1fd4f4733eacf11dba9"), \n',
' "centralPath" : "XXX2", \n',
' "viewStats" : [\n',
' {\n',
' "totalViews" : NumberInt(3642), \n',
' "totalSheets" : NumberInt(393), \n',
' "totalSchedules" : NumberInt(427), \n',
' "viewsOnSheet" : NumberInt(1949), \n',
' "viewsOnSheetWithTemplate" : NumberInt(625), \n',
' "schedulesOnSheet" : NumberInt(371), \n',
' "unclippedViews" : NumberInt(876), \n',
' "createdOn" : ISODate("2017-10-13T18:06:45.291+0000"), \n',
' "_id" : ObjectId("59e100b535eeefcc27ee0802")\n',
' }, \n',

我尝试了 read_json/https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html 上报告的所有不同方法和解决方案和加载/加载(str)等,但什么也没有。

最佳答案

问题出在 JSON 文件的格式上,我们用https://jsonformatter.curiousconcept.com/进行了测试并使用正则表达式进行修改如果您有更好的建议请告诉我。

导入重新

with open("views3.json", "r+") as read_file:
data = read_file.read()
x = re.sub("\w+\((.+)\)", r'\1', data)
print(x)

read_file.close

关于json - 预期对象或值 : Importing JSON into Python (Pandas),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com