我正在使用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上报告的所有不同方法和解决方案 和load / load(str)等。
答案 0 :(得分:0)
您想要吗?:使用modul json读取file.json
import pandas as pd
import json
with open('file.json') as viewsmc:
data = json.load(viewsmc)
print data #you have a dict
df = pd.DataFrame(data)
print(df)
答案 1 :(得分:0)
问题出在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.closed