简体   繁体   English

使用 python 读取 json 文件

[英]Read a json file using python

with open('file.json', "r") as input_file:    
    data = json.load(input_file)
print(data)

This is my piece of code of reading a json file.这是我读取 json 文件的代码。 It works fine with I run it in a.py, but it gave me error when i put it on Jupyter Notebook as follows:我在 a.py 中运行它可以正常工作,但是当我将它放在 Jupyter Notebook 上时它给了我错误,如下所示:

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

The json file looks like this: json 文件如下所示: 在此处输入图像描述 Can anyone help me with it.任何人都可以帮助我。 Thanks谢谢

You need read() to import the json into your file according to this:您需要 read() 根据以下内容将 json 导入到您的文件中:

with open('sample_tweets.json', "r") as input_file:    
    data = json.load(input_file.read())

I could not verify it with your json file, but that is the way it works for me.我无法使用您的 json 文件验证它,但这就是它适用于我的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM