簡體   English   中英

無法加載 json 文件,大量對象,在 python 中創建 dataframe

[英]unable to load json file, huge array of objects, to create a dataframe in python

我正在嘗試加載一個 json 文件,該文件在數組中有大約 2k 個對象:

[
  {
    "id": 5375,
    "name": "cepharanthine",
    "mrdef": "The mechanism of action of cepharanthine is multifactorial. The drug exerts membrane effects (modulation of efflux pumps, membrane rigidification) as well as different intracellular and nuclear effects. Cepharanthine interferes with several metabolic axes, primarily with the AMP-activated protein kinase (AMPK) and NFkappaB signaling pathways. In particular, the anti-inflammatory effects of cepharanthine rely on AMPK activation and NFkappaB inhibition.",
    "indications": [
      "Leukopenia",
      " Snake bite - wound",
      " Aptyalism",
      " Alopecia"
    ],
    "contraindication": [
      ""
    ]
  },
  {
    "id": 5301,
    "name": "baloxavir marboxil",
    "mrdef": "Baloxavir marboxil is a prodrug that is converted by hydrolysis to baloxavir, the active form that exerts anti-influenza virus activity. Baloxavir inhibits the endonuclease activity of the polymerase acidic (PA) protein, an influenza virus-specific enzyme in the viral RNA polymerase complex required for viral gene transcription, resulting in inhibition of influenza virus replication. The 50% inhibitory concentration (IC50) of baloxavir was 1.4 to 3.1 nM (n=4) for influenza A viruses and 4.5 to 8.9 nM (n=3) for influenza B viruses in a PA endonuclease assay. Viruses with reduced susceptibility to baloxavir have amino acid substitutions in the PA protein.",
    "indications": [
      "Influenza"
    ],
    "contraindication": [
      ""
    ]
  },
....

我正在嘗試像這樣加載文件:

import json

with open('filepath', 'r') as f:
    data = json.load(f)
    print(data)

但我得到的錯誤是:

JSONDecodeError                           Traceback (most recent call last)
<ipython-input-81-50fc0a9fd23c> in <module>
      1 with open('C:/Users/Mohammed Safee Uddin/Documents/drugcentral_generics_data.json', 'r') as f:
----> 2     data = json.load(f)
      3     print(data)

~\anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,
--> 296         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    297 
    298 

~\anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~\anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我也嘗試將 json.load 更改為 json.loads(f.read()) ,但沒有任何效果,它拋出相同的錯誤。 我是編程本身的新手,非常感謝任何幫助。 提前致謝。

有 2 種(主要)方法可以加載 json 文件。 使用“r”將文件作為文本閱讀器打開。 在這種情況下,您需要調用f.read()並使用json.loads()進行轉換

with open('data.txt', 'r') as f:
    data = json.loads(f.read())
    print(data)

或者,您可以在不使用閱讀器模式'r'情況下打開文件並調用json.load()

with open('data.txt') as json_file:
    data = json.load(json_file)
    print(data)

暫無
暫無

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

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