繁体   English   中英

ValueError:预期为 object 或将 json 读取为 pandas dataframe 时的值

[英]ValueError: Expected object or value when reading json as pandas dataframe

样本数据

{
   "_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"
}

我试过了:

import pandas as pd
data= pd.read_json('Data.json')

收到错误 ValueError: Expected object or value

import json
with open('gdb.json') as datafile:
    data = json.load(datafile)
retail = pd.DataFrame(data)

错误:json.decoder.JSONDecodeError:额外数据:第 2 行第 1 列(字符 509)

with open('gdb.json') as datafile:
for line in datafile:
    data = json.loads(line)
retail = pd.DataFrame(data)

错误:json.decoder.JSONDecodeError:额外数据:第 1 行第 577 列(字符 576)

如何将这个 json 读成 pandas

我遇到了同样的错误,阅读函数文档并使用不同的参数。

我通过使用下面的一个解决了它,

data= pd.read_json('Data.json', lines=True)

你可以尝试其他的东西,比如

data= pd.read_json('Data.json', lines=True, orient='records')

data= pd.read_json('Data.json', orient=str)

您的 JSON 格式错误。

ValueError: Expected object or value如果您输入错误的文件名,可能会出现ValueError: Expected object or value Data.json是否存在? 我注意到您在其他尝试中使用了gdb.json

确认文件名正确后,您必须修复 JSON。 您现在拥有的是两个由空格分隔的断开连接的记录。 JSON 中的列表必须是方括号内的有效数组并用逗号分隔: [{record1}, {record2}, ...]

此外,对于熊猫,您应该将数组放在名为"data"的根元素下:

{ "data": [ {record1}, {record2}, ... ] }

您的 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"
    }]}

最后,pandas 将此格式称为split orientation ,因此您必须按如下方式加载它:

df = pd.read_json('gdb.json', orient='split')

df现在包含以下数据框:

          _id                                                   categories  isActive                                                     imageUrl        barcode  isFmcg                           itemName  mrp                      _created_at                      _updated_at
0  OzE5vaa3p7  [{'__type': 'Pointer', 'className': 'Category', 'objectI...      True  https://firebasestorage.googleapis.com/v0/b/shopgro-1376...  8908001921015    True              Anil puttu flour 500g   58 2016-10-02 13:49:03.281000+00:00 2017-02-22 08:48:09.548000+00:00
1  ENPCL8ph1p  [{'__type': 'Pointer', 'className': 'Category', 'objectI...      True  https://firebasestorage.googleapis.com/v0/b/kirananearby...  8901725181222    True  Yippee Magic Masala Noodles, 70 G   12 2016-10-02 13:49:03.284000+00:00 2017-02-22 08:48:09.074000+00:00

你应该确保终端目录与文件目录相同(当我发生这个错误时,因为我使用了vscode,对我来说意味着vscode中的终端目录与我想要的python文件不同执行)

我不认为这会是问题,因为它应该是默认值(我认为)。 但是你试过这个吗? 添加“r”以指定文件是只读的。

import json with open('gdb.json', 'r') as datafile: data = json.load(datafile) retail = pd.DataFrame(data)

使您的路径变得轻松,这将有助于阅读数据。 同时,只需将您的文件放在桌面上并提供读取数据的路径。 有用。

您可以尝试将相对路径更改为绝对路径针对您的情况更改

import pandas as pd
data= pd.read_json('Data.json')

import pandas as pd
data= pd.read_json('C://Data.json')#the absolute path in explore

当我在控制台中从 jupyter notebook 运行相同的代码到 pycharm 的 jupyter notebook 时,我遇到了同样的错误

另一种变体,结合了线程中所有独立失败的提示,但这对我有用:

pd.read_json('file.json', lines=True, encoding = 'utf-8-sig')

如果您输入绝对路径并使用 \ 它应该可以工作。 至少我就是这样解决问题的

我不确定我是否清楚地理解了您的问题,您只是想读取 json 数据?

我刚刚将您的样本数据收集到列表中,如下所示

[
  {
   "_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"
}
]

并运行此代码

import pandas as pd
df = pd.read_json('Data.json')
print(df)

输出:-

              _created_at ... mrp
0 2016-10-02 13:49:03.281 ...  58
1 2016-10-02 13:49:03.284 ...  12

[2 rows x 10 columns]

如果您尝试下面的代码,它将解决问题:

data_set = pd.read_json(r'json_file_address\file_name.json', lines=True)

我今天遇到了这个错误消息,就我而言,问题是文本文件的编码是 UTF-8-BOM 而不是 UTF-8,这是 read_json() 的默认值。 这可以通过指定编码来解决:

data= pd.read_json('Data.json', encoding = 'utf-8-sig')

我遇到了同样的问题,这背后的原因是 json 文件有一些不遵守 json 规则的东西。 在我的情况下,我在其中一个值中使用了单引号而不是双引号。

在此处输入图片说明

这对我有用: pd.read_json('./dataset/healthtemp.json', typ="series")

除了一件事,一切都很好

在 .json 文件中输入以下代码:

{
"a": {
    "_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"
},
"b": {
    "_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"
}
}

谢谢

ValueError: All arrays must be of the same length

df = pd.read_json (r'./filename.json')#,lines=True)

可以通过将上面的行更改为以下行来解决。

df = pd.read_json (r'./filename.json',lines=True)

我只是通过在绝对路径的开头添加一个“/”来解决这个问题。

import pandas as pd    
pd_from_json = pd.read_json("/home/miguel/folder/information.json")

似乎有一百万种事情会导致这种情况。 在我的例子中,我的 json 文件开始有一个字节顺序标记,在 vim-airline 中用[BOM] [unix]表示。 我不知道字节顺序标记是什么或何时需要它。 为了删除它,在 vim 中,我运行了:set nobomb然后保存了文件。 然后,pandas 可以读取它,我对 go 很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM