【问题标题】:JSON pandas dataframe ValueError: Expected object or valueJSON pandas dataframe ValueError:预期的对象或值
【发布时间】:2022-11-30 16:06:39
【问题描述】:

我正在尝试使用 pandas 读取 JSON 文件。 JSON 文件采用以下格式:

{
    "category": "CRIME", 
    "headline": "There Were 2 Mass Shootings In Texas Last Week, But Only 1 On TV", 
    "authors": "Melissa Jeltsen", 
    "link": "https://www.huffingtonpost.com/entry/texas-amanda-painter-mass-shooting_us_5b081ab4e4b0802d69caad89", "short_description": "She left her husband. He killed their children. Just another day in America.", 
    "date": "2018-05-26"
}
{
    "category": "ENTERTAINMENT", 
    "headline": "Will Smith Joins Diplo And Nicky Jam For The 2018 World Cup's Official Song", 
    "authors": "Andy McDonald", 
    "link": "https://www.huffingtonpost.com/entry/will-smith-joins-diplo-and-nicky-jam-for-the-official-2018-world-cup-song_us_5b09726fe4b0fdb2aa541201", 
    "short_description": "Of course, it has a song.", 
    "date": "2018-05-26"
}

但是,我收到以下错误,我不明白为什么:

ValueError                                Traceback (most recent call last)
/var/folders/j6/rj901v4j40368zfdw64pbf700000gn/T/ipykernel_11792/4234726591.py in <module>
----> 1 df = pd.read_json('db.json', lines=True)
      2 df.head()

~/opt/anaconda3/lib/python3.9/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    205                 else:
    206                     kwargs[new_arg_name] = new_arg_value
--> 207             return func(*args, **kwargs)
    208 
    209         return cast(F, wrapper)

~/opt/anaconda3/lib/python3.9/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

~/opt/anaconda3/lib/python3.9/site-packages/pandas/io/json/_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, encoding_errors, lines, chunksize, compression, nrows, storage_options)
    610 
    611     with json_reader:
--> 612         return json_reader.read()
    613 
    614 

~/opt/anaconda3/lib/python3.9/site-packages/pandas/io/json/_json.py in read(self)
    742                 data = ensure_str(self.data)
    743                 data_lines = data.split("\n")
--> 744                 obj = self._get_object_parser(self._combine_lines(data_lines))
    745         else:
    746             obj = self._get_object_parser(self.data)

~/opt/anaconda3/lib/python3.9/site-packages/pandas/io/json/_json.py in _get_object_parser(self, json)
    766         obj = None
    767         if typ == "frame":
--> 768             obj = FrameParser(json, **kwargs).parse()
    769 
    770         if typ == "series" or obj is None:

~/opt/anaconda3/lib/python3.9/site-packages/pandas/io/json/_json.py in parse(self)
    878             self._parse_numpy()
    879         else:
--> 880             self._parse_no_numpy()
    881 
    882         if self.obj is None:

~/opt/anaconda3/lib/python3.9/site-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
   1131         if orient == "columns":
   1132             self.obj = DataFrame(
-> 1133                 loads(json, precise_float=self.precise_float), dtype=None
   1134             )
   1135         elif orient == "split":

ValueError: Expected object or value

我的代码是这样写的:

import pandas as pd

df = read_json('db.json', lines=True)
df.head()

我尝试按照 here 的建议更改 JSON 文件的结构,但它不起作用。我得到的错误与我在上面指定的错误相同。还有其他方法可以解决这个问题吗?

【问题讨论】:

    标签: python json pandas dataframe


    【解决方案1】:

    您可以将其包裹在方括号 [] 中,并在有效 json 的字典之间添加一个逗号。

    [{
        "category": "CRIME",
        "headline": "There Were 2 Mass Shootings In Texas Last Week, But Only 1 On TV",
        "authors": "Melissa Jeltsen",
        "link": "https://www.huffingtonpost.com/entry/texas-amanda-painter-mass-shooting_us_5b081ab4e4b0802d69caad89", "short_description": "She left her husband. He killed their children. Just another day in America.",
        "date": "2018-05-26"
    },
    {
        "category": "ENTERTAINMENT",
        "headline": "Will Smith Joins Diplo And Nicky Jam For The 2018 World Cup's Official Song",
        "authors": "Andy McDonald",
        "link": "https://www.huffingtonpost.com/entry/will-smith-joins-diplo-and-nicky-jam-for-the-official-2018-world-cup-song_us_5b09726fe4b0fdb2aa541201",
        "short_description": "Of course, it has a song.",
        "date": "2018-05-26"
    }]
    

    【讨论】:

      猜你喜欢
      • 2020-12-28
      • 2021-09-26
      • 2019-07-27
      • 2016-05-24
      • 2017-11-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多