簡體   English   中英

讀取 python 中的 json 文件時預期 object 或值錯誤

[英]Expected object or value error while reading json file in python

path = '/content/drive/MyDrive/gbd.json'
df = pd.read_json(path)

this is the error I am getting.


ValueError                                Traceback (most recent call last)
<ipython-input-11-3e269770844d> in <module>()
      1 path = '/content/drive/MyDrive/gbd.json'
----> 2 df = pd.read_json(path)

6 frames
/usr/local/lib/python3.7/dist-packages/pandas/io/json/_json.py in _parse_no_numpy(self)
   1138         if orient == "columns":
   1139             self.obj = DataFrame(
-> 1140                 loads(json, precise_float=self.precise_float), dtype=None
   1141             )
   1142         elif orient == "split":

ValueError: Expected object or value

我已經嘗試了以下所有解決方案

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)

this is the link to the json file https://ccewuksprdoneregsadata1.blob.core.windows.net/data/json/publicextract.charity.zip I want to create a dataframe from the json file

我能夠制作一個 DataFrame 與您的文件的一小部分提取,問題是在文件的開頭有一小部分不可見的字符組,它以某種方式阻止了它。 它不會對所有編輯器都可見,我用不同的編輯器測試它是純粹的運氣。 這個字符看起來像 ,嘗試用不同的編輯器打開它並刪除它,它會起作用。

文件格式不正確。 嘗試:

with open('publicextract.charity.json', encoding='utf-8-sig') as fp:
    data = json.loads(''.join(line.strip() for line in fp))

df = pd.json_normalize(data)

Output:

            date_of_extract  organisation_number  registered_charity_number  linked_charity_number                        charity_name  ... cio_is_dissolved date_cio_dissolution_notice charity_activities charity_gift_aid charity_has_land
0       2022-02-02T00:00:00                    1                     200027                      1     POTTERNE MISSION ROOM AND TRUST  ...             None                        None               None             None             None
1       2022-02-02T00:00:00                    2                     200027                      2                 HITCHAM FREE CHURCH  ...             None                        None               None             None             None
2       2022-02-02T00:00:00                    3                     200028                      1     TOWN LANDS CHARITY FOR THE POOR  ...             None                        None               None             None             None
3       2022-02-02T00:00:00                    4                     200028                      2   TOWN LANDS CHARITY FOR THE CHURCH  ...             None                        None               None             None             None
4       2022-02-02T00:00:00                    5                     200034                      1     CLOPHILL RELIEF IN NEED CHARITY  ...             None                        None               None             None             None
...                     ...                  ...                        ...                    ...                                 ...  ...              ...                         ...                ...              ...              ...
376732  2022-02-02T00:00:00              5193574                    1197688                      0      FEN DRAYTON PRIMARY SCHOOL PTA  ...            False                        None               None             None             None
376733  2022-02-02T00:00:00              5193708                    1197741                      0  BOLSOVER C OF E JUNIOR SCHOOL FROG  ...            False                        None               None             None             None
376734  2022-02-02T00:00:00              5193765                    1197681                      0         ST MICHAEL'S RC PRIMARY PTA  ...            False                        None               None             None             None
376735  2022-02-02T00:00:00              5193830                    1197732                      0              THE LIGHTHOUSE, DARWEN  ...            False                        None               None             None             None
376736  2022-02-02T00:00:00              5193998                    1197750                      0               THE PENDLE FOUNDATION  ...            False                        None               None             None             None

[376737 rows x 34 columns]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM