使用python解析JSON文件中的数据并进行检查
我有一个JSON文件,其中包含以下内容:使用python解析JSON文件中的数据并进行检查,python,json,Python,Json,我有一个JSON文件,其中包含以下内容: { "jeff": { } } 我想检查一下我是否在输入中输入了jeff的名字,它只会打印123 我在python中尝试过这种方法: import json from pprint import pprint with open('users.json') as f: data = json.load(f) print("Hi, would you like to sign up or login?") print("Ty
{
"jeff": {
}
}
我想检查一下我是否在输入中输入了jeff的名字,它只会打印123
我在python中尝试过这种方法:
import json
from pprint import pprint
with open('users.json') as f:
data = json.load(f)
print("Hi, would you like to sign up or login?")
print("Type Login for login and Signup to signup")
option= input()
if option == "Login":
unameEntry = input("Please Enter Your Username")
if unameEntry == data[unameEntry] :
print("123")
但它没有找到它我在谷歌上四处寻找,并努力寻找答案怎么了
使用以下工具访问词典中的值时:
data[unameEntry]
字典将返回数据[unmeentry]
处的值。假设,正如您在示例中所设置的,用户在“jeff”
中键入:在您的示例中,字典将返回相应的值,在您的示例中是一个空字典
因此,您的if实际上正在做:
"jeff" == {}
这显然是错误的
如何修复它
您很可能希望检查用户是否是字典中的键。您可以使用中的操作符来执行此操作:
unameEntry in data
编辑代码
因为在这里我们无法访问您的users.json
文件,所以让我们在代码中包含字典。此外,为了检测是否在输入处理中发现错误,我将在两个断言中进行检查并展开条件树:
data = {
"jeff": {}
}
print("Hi, would you like to sign up or login?")
print("Type Login for login and Signup to signup")
option= input()
assert option == "Login"
if option == "Login":
unameEntry = input("Please Enter Your Username")
assert unameEntry == "jeff"
if unameEntry in data:
print("Welcome %s!" % unameEntry)
else:
print("Username %s does not exist" % unameEntry)
对现有内容进行了一些调整,并添加了一些详细注释。下面是Python的外观:
# Python3
import json
# Load json file one-liner
file_json = json.load(open("test.json", "r"))
# Since options can only be signup/login, santize them
option = input("Type Login for login and Signup to signup: ").strip().lower()
if option == "login":
# Get name entry and strip it of whitespace
name_entry = input("Please Enter Your Username: ").strip()
# Use .get() on file_json dict() object, with a default option
# of None. However, with this you actually have to add data to
# your JSON and not just a empty field
if file_json.get(name_entry, None):
print("123")
else:
print("Name: {}, not found".format(name_entry))
else:
print("unrecognized option: {}".format(option))
{
"jeff": {
"foo": "bar"
}
}
测试JSON应该是这样的:
# Python3
import json
# Load json file one-liner
file_json = json.load(open("test.json", "r"))
# Since options can only be signup/login, santize them
option = input("Type Login for login and Signup to signup: ").strip().lower()
if option == "login":
# Get name entry and strip it of whitespace
name_entry = input("Please Enter Your Username: ").strip()
# Use .get() on file_json dict() object, with a default option
# of None. However, with this you actually have to add data to
# your JSON and not just a empty field
if file_json.get(name_entry, None):
print("123")
else:
print("Name: {}, not found".format(name_entry))
else:
print("unrecognized option: {}".format(option))
{
"jeff": {
"foo": "bar"
}
}
请注意,我是如何将默认值添加到“jeff”
。否则,当您获得{}
作为“jeff”
的值时,执行查找将始终不返回任何内容我将其更改为this if unmeentry in data:print(“123”)。我已编辑代码以帮助您进一步调试。现在怎么样?嗨,你想注册还是登录?请输入您的用户名jeff用户名jeff不存在]>>>我添加了另一个断言,它现在是否引发异常?