首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ValueError:预期对象或值

ValueError:预期对象或值
EN

Stack Overflow用户
提问于 2022-11-30 07:37:23
回答 1查看 22关注 0票数 0

我试着用熊猫读一个JSON文件。JSON文件的格式如下:

代码语言:javascript
运行
复制
{
    "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"
}

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

代码语言:javascript
运行
复制
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

我的代码如下所示:

代码语言:javascript
运行
复制
import pandas as pd

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-30 07:46:30

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

代码语言:javascript
运行
复制
[{
    "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"
}]

阅读档案::

代码语言:javascript
运行
复制
import pandas as pd


df = pd.read_json("/path/to/file/db.json")
print(df)

输出:

代码语言:javascript
运行
复制
        category                                                                     headline          authors                                                                                                 link                                                             short_description       date
0          CRIME             There Were 2 Mass Shootings In Texas Last Week, But Only 1 On TV  Melissa Jeltsen  https://www.huffingtonpost.com/entry/texas-amanda-painter-mass-shooting_us_5b081ab4e4b0802d69caad89  She left her husband. He killed their children. Just another day in America. 2018-05-26
1  ENTERTAINMENT  Will Smith Joins Diplo And Nicky Jam For The 2018 World Cup's Official Song    Andy McDonald  https://www.huffingtonpost.com/entry/will-smith-joins-diplo-and-nicky-jam-for-the-official-2018-...                                                     Of course, it has a song. 2018-05-26
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74624275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档