我是 Python 新手,我正在尝试从我的网站获取大量用户信息并保存每个人拥有的用户组
我已经能够将响应保存为 json,但我不知道如何读取返回的内容
我的 json 文件
{
"pagination": {
"current_page": 1,
"last_page": 9,
"per_page": 20,
"shown": 20,
"total": 173
},
我不想显示太多,因为我的文件中保存了电子邮件等。
具体来说,我希望保存“total”的值
尝试
with open(pathFile) as json_file:
# read json file line by line
for line in json_file.readlines():
# create python dict from json object
json_dict = json.loads(line)
# check if "body" (lowercased) contains any of the keywords
if any(keyword in json_dict["total"].lower() for keyword in keywords):
print(json_dict["pagination"])
错误消息
Exception has occurred: JSONDecodeError
Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
File "C:\Users\New User\Desktop\path thing", line 43, in <module>
json_dict = json.loads(line)
最佳答案
不要尝试读取每一行,只需在整个文件上使用 json.load 即可。
import json
with open(filePath, "r") as json_file:
data = json.load(json_file)
total = data["pagination"]["total"]
print(total)
关于python - 从 json 文件中读取 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66373503/