【问题标题】:Error when reading json file in python: Expected object or value在 python 中读取 json 文件时出错:预期的对象或值
【发布时间】:2019-10-19 06:07:11
【问题描述】:

我将这个 json 文件保存在我想要阅读的 jupyter 笔记本中:

[
    {   'category_type': 'c',
        'modification_type': 'fixed',
        'modification_list': 0 ,
        'features_list': ['feature1','feature2']
    },
    {   'category_type': 'continious',
        'modification_type': 'fixed_list',
        'modification_list': 1 ,
        'suffix': '_1',
        'features_list': ['feature3','feature4']
    }
]

该文件保存在 jupyter notebook 主页的文件夹中,我正在尝试使用以下代码读取它:

pd.read_json('folder/file.json', orient='records',encoding='utf-8')

但我不断收到此错误:ValueError: Expected object or value

谁能帮忙解决这个问题?

【问题讨论】:

  • 那不是 JSON。 JSON 使用双引号,而不是单引号。
  • 即使我用双引号更改单引号,我也会得到同样的错误..
  • 根据jsonformatter.curiousconcept.com,如果您将单引号更改为双引号,则它是 RFC 7159 的有效 json
  • 该链接中的解决方案都没有真正起作用....

标签: python json


【解决方案1】:

要将 json 读取为 pandas 数据框,只需使用这几行代码。我认为这是你的问题

import pandas as pd
import numpy as np


# Load the first sheet of the JSON file into a data frame
data_df = pd.read_json('reviewsclean.json', lines=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2021-08-07
    • 2019-04-27
    • 1970-01-01
    • 2021-06-16
    相关资源
    最近更新 更多