adi*_*nkv 7 python json pandas
样本数据:
{
"_id": "OzE5vaa3p7",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "nebCwWd2Fr"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
"barcode": "8908001921015",
"isFmcg": true,
"itemName": "Anil puttu flour 500g",
"mrp": 58,
"_created_at": "2016-10-02T13:49:03.281Z",
"_updated_at": "2017-02-22T08:48:09.548Z"
}
{
"_id": "ENPCL8ph1p",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "B4nZeUHmVK"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
"barcode": "8901725181222",
"isFmcg": true,
"itemName": "Yippee Magic Masala Noodles, 70 G",
"mrp": 12,
"_created_at": "2016-10-02T13:49:03.284Z",
"_updated_at": "2017-02-22T08:48:09.074Z"
}
Run Code Online (Sandbox Code Playgroud)
我试过了:
import pandas as pd
data= pd.read_json('Data.json')
Run Code Online (Sandbox Code Playgroud)
获取错误ValueError:预期的对象或值
也
import json
with open('gdb.json') as datafile:
data = json.load(datafile)
retail = pd.DataFrame(data)
Run Code Online (Sandbox Code Playgroud)
错误:json.decoder.JSONDecodeError:额外数据:第2行第1列(char 509)
with open('gdb.json') as datafile:
for line in datafile:
data = json.loads(line)
retail = pd.DataFrame(data)
Run Code Online (Sandbox Code Playgroud)
错误:json.decoder.JSONDecodeError:额外数据:第1行第577行(字符576)
如何将这个json读成大熊猫
您的 JSON 格式错误。
ValueError: Expected object or value
如果您输入错误的文件名,可能会发生。是否Data.json
存在?我注意到你使用的其他尝试gdb.json
。
确认文件名正确后,您必须修复 JSON。您现在拥有的是两个由空格分隔的断开连接的记录。JSON 中的列表必须是方括号内的有效数组,并用逗号分隔:[{record1}, {record2}, ...]
此外,对于熊猫,您应该将数组放在名为 的根元素下"data"
:
{ "data": [ {record1}, {record2}, ... ] }
Run Code Online (Sandbox Code Playgroud)
您的 JSON 最终应如下所示:
{"data":
[{
"_id": "OzE5vaa3p7",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "nebCwWd2Fr"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
"barcode": "8908001921015",
"isFmcg": true,
"itemName": "Anil puttu flour 500g",
"mrp": 58,
"_created_at": "2016-10-02T13:49:03.281Z",
"_updated_at": "2017-02-22T08:48:09.548Z"
}
,
{
"_id": "ENPCL8ph1p",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "B4nZeUHmVK"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
"barcode": "8901725181222",
"isFmcg": true,
"itemName": "Yippee Magic Masala Noodles, 70 G",
"mrp": 12,
"_created_at": "2016-10-02T13:49:03.284Z",
"_updated_at": "2017-02-22T08:48:09.074Z"
}]}
Run Code Online (Sandbox Code Playgroud)
最后,pandas 调用此格式split orientation
,因此您必须按如下方式加载它:
{ "data": [ {record1}, {record2}, ... ] }
Run Code Online (Sandbox Code Playgroud)
df
现在包含以下数据框:
{"data":
[{
"_id": "OzE5vaa3p7",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "nebCwWd2Fr"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/shopgro-1376.appspot.com/o/Barcode%20Data%20Upload%28II%29%2FAnil_puttu_flour_500g.png?alt=media&token=9cf63197-0925-4360-a31a-4675f4f46ae2",
"barcode": "8908001921015",
"isFmcg": true,
"itemName": "Anil puttu flour 500g",
"mrp": 58,
"_created_at": "2016-10-02T13:49:03.281Z",
"_updated_at": "2017-02-22T08:48:09.548Z"
}
,
{
"_id": "ENPCL8ph1p",
"categories": [
{
"__type": "Pointer",
"className": "Category",
"objectId": "B4nZeUHmVK"
}
],
"isActive": true,
"imageUrl": "https://firebasestorage.googleapis.com/v0/b/kirananearby-9eaa8.appspot.com/o/Barcode%20data%20upload%2FYippee_Magic_Masala_Noodles,_70_g.png?alt=media&token=d9e47bd7-f847-4d6f-9460-4be8dbcaae00",
"barcode": "8901725181222",
"isFmcg": true,
"itemName": "Yippee Magic Masala Noodles, 70 G",
"mrp": 12,
"_created_at": "2016-10-02T13:49:03.284Z",
"_updated_at": "2017-02-22T08:48:09.074Z"
}]}
Run Code Online (Sandbox Code Playgroud)
我今天遇到了这个错误消息,在我的例子中,问题是文本文件的编码是 UTF-8-BOM 而不是 UTF-8,这是 read_json() 的默认编码。可以通过指定编码来解决:
data= pd.read_json('Data.json', encoding = 'utf-8-sig')
Run Code Online (Sandbox Code Playgroud)
小智 2
我不认为这会是问题,因为它应该是默认的(我认为)。但你尝试过这个吗?添加“r”以指定该文件是只读的。
import json
with open('gdb.json', 'r') as datafile:
data = json.load(datafile)
retail = pd.DataFrame(data)