python - 无法加载 json 文件,大量对象,以在 python 中创建数据框
问题描述
我正在尝试加载一个数组中包含大约 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()) 但没有任何效果,它会抛出相同的错误。我是编程本身的新手,非常感谢任何帮助。提前致谢。
解决方案
有 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)
推荐阅读
- python - 是否可以将 FacetGrid 或 relplot 用于带有标签的多个时间序列线图?
- angular - PrimeNG:拦截器中的 DialogService
- javascript - 如何使用 jQuery 验证整个表单
- java - 静态常量在匿名类中有何用处?
- machine-learning - 带有一个 for 循环的 KNN 距离函数
- javascript - 如何在邮递员测试主体的另一个请求测试主体中使用一个请求中的变量?
- python - 使用python从csv文件中读取复数
- c++ - 使用 mongocxx 驱动程序的 Visual Studio 2019 x64 应用程序
- node.js - 如何将假设的 JavaScript 库转换为可发布的 NPM 包?
- keras - Keras中二进制交叉熵损失值的含义?