当我尝试将JSON文件作为文件读取时出错

2024-10-01 22:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将JSON文件作为pd数据帧加载,但是我遇到了这种回溯

Traceback (most recent call last):
  File "C:\Users\deivs\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-9e0d5e746a54>", line 1, in <module>
    runfile('C:/Users/deivs/PycharmProjects/COVID_API/Tests_on_covid_data.py', wdir='C:/Users/deivs/PycharmProjects/COVID_API')
  File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/deivs/PycharmProjects/COVID_API/Tests_on_covid_data.py", line 5, in <module>
    df = pd.read_json('C:/Users/deivs/OneDrive/Desktop/Covid_state_data.json')
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\util\_decorators.py", line 214, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\io\json\_json.py", line 608, in read_json
    result = json_reader.read()
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\io\json\_json.py", line 731, in read
    obj = self._get_object_parser(self.data)
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\io\json\_json.py", line 753, in _get_object_parser
    obj = FrameParser(json, **kwargs).parse()
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\io\json\_json.py", line 857, in parse
    self._parse_no_numpy()
  File "C:\Users\deivs\anaconda3\lib\site-packages\pandas\io\json\_json.py", line 1089, in _parse_no_numpy
    loads(json, precise_float=self.precise_float), dtype=None
ValueError: Expected object or value

我使用的代码如下

import numpy as np
import pandas as pd
import json

df = pd.read_json('C:/Users/deivs/OneDrive/Desktop/Covid_state_data.json')
df.head()

我的json文件格式正确(我在JSONLINT上检查过)

[
    {

        "date": 20200402,
        "state": "AL",
        "positive": 1233,
        "negative": 7503,
        "pending": null,
        "hospitalizedCurrently": null,
        "hospitalizedCumulative": null,
        "inIcuCurrently": null,
        "inIcuCumulative": null,
        "onVentilatorCurrently": null,
        "onVentilatorCumulative": null,
        "recovered": null,
        "dataQualityGrade": "B",
        "lastUpdateEt": "4/2/2020 00:00",
        "hash": "3b7e06a8f363841e0c01239ec822fb61543caa33",
        "dateChecked": "2020-04-02T20:00:00Z",
        "death": 32,
        "hospitalized": null,
        "total": 8736,
        "totalTestResults": 8736,
        "posNeg": 8736,
        "fips": "01",
        "deathIncrease": 6,
        "hospitalizedIncrease": 0,
        "negativeIncrease": 806,
        "positiveIncrease": 156,
        "totalTestResultsIncrease": 962
    },...]

很抱歉这么长时间,但我不确定出了什么问题


Tags: inpyselfjsonpandaslibpackagesline
1条回答
网友
1楼 · 发布于 2024-10-01 22:34:41

如果这是您试图读取的同一个文件(假设“},…]”表示更多有效记录),那么它对我来说工作正常。这就是我所做的

sample.json:

[
    {
        "date": 20200402,
        "state": "AL",
        "positive": 1233,
        "negative": 7503,
        "pending": null,
        "hospitalizedCurrently": null,
        "hospitalizedCumulative": null,
        "inIcuCurrently": null,
        "inIcuCumulative": null,
        "onVentilatorCurrently": null,
        "onVentilatorCumulative": null,
        "recovered": null,
        "dataQualityGrade": "B",
        "lastUpdateEt": "4/2/2020 00:00",
        "hash": "3b7e06a8f363841e0c01239ec822fb61543caa33",
        "dateChecked": "2020-04-02T20:00:00Z",
        "death": 32,
        "hospitalized": null,
        "total": 8736,
        "totalTestResults": 8736,
        "posNeg": 8736,
        "fips": "01",
        "deathIncrease": 6,
        "hospitalizedIncrease": 0,
        "negativeIncrease": 806,
        "positiveIncrease": 156,
        "totalTestResultsIncrease": 962
    }
]

代码:

df = pd.read_json("sample.json").T

print(df)

输出:

                                                                 0
dataQualityGrade                                                 B
date                                                      20200402
dateChecked                                   2020-04-02T20:00:00Z
death                                                           32
deathIncrease                                                    6
fips                                                             1
hash                      3b7e06a8f363841e0c01239ec822fb61543caa33
hospitalized                                                   NaN
hospitalizedCumulative                                         NaN
hospitalizedCurrently                                          NaN
hospitalizedIncrease                                             0
inIcuCumulative                                                NaN
inIcuCurrently                                                 NaN
lastUpdateEt                                        4/2/2020 00:00
negative                                                      7503
negativeIncrease                                               806
onVentilatorCumulative                                         NaN
onVentilatorCurrently                                          NaN
pending                                                        NaN
posNeg                                                        8736
positive                                                      1233
positiveIncrease                                               156
recovered                                                      NaN
state                                                           AL
total                                                         8736
totalTestResults                                              8736
totalTestResultsIncrease                                       962

相关问题 更多 >

    热门问题