【发布时间】: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