【问题标题】:unable to load json file, huge array of objects, to create a dataframe in python无法加载 json 文件,大量对象,以在 python 中创建数据框
【发布时间】:2020-07-23 19:00:42
【问题描述】:

我正在尝试加载一个数组中包含大约 2k 个对象的 json 文件:

[
  {
    "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()) 但没有任何效果,它会抛出相同的错误。我是编程本身的新手,非常感谢任何帮助。提前致谢。

【问题讨论】:

  • 你试过data = json.load(f.read())
  • 是的,我试过了,但它给出了以下错误:``` AttributeError: 'str' object has no attribute 'read' ```
  • stackoverflow.com/a/34010821/8265036 这能回答你的问题吗?
  • 我假设 'filepath' 被替换为实际的文件名和路径?
  • 该链接中的@FarhoodET 解决方案已经奏效,但恐怕它已将整个数据更改为字符串,我该如何从中创建数据框?

标签: python json python-3.x jupyter-notebook


【解决方案1】:

有 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)

【讨论】:

  • 感谢@Greg,我将文件更改为 .txt 并按照您的代码进行操作,它们都运行良好。出于好奇,这是否意味着我们不能直接上传 .json 文件.... :(
  • data.txt 应该是 data.json。据我所知,Python open 并不关心文件扩展名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 2020-09-29
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
相关资源
最近更新 更多