我如何将 JSON true 或 false 连接到我的 python 脚本
How can i connect JSON true or false to my python script
你好,我正在使用 python 并想将 JSON True 或 False 连接到我的代码,所以如果“dummy”:“True”做...否则:做...
我的 JSON
[
{
"profilename": "Test3",
"email": "email@outlook.com",
"password": "Password2*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"False"
},
{
"profilename": "Test1",
"email": "email@gmail.com",
"password": "Password1*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"True"
}
]
我的python代码:
profile_name = "Test3"
with open('Data2.json', 'r') as handle:
json = json.load(handle)
data = [x for x in json if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['dummy'])
def getLogin():
if dummy = "True": <----- This doesnt work, want to get this fixed
import Script2
else:
print("starting login process")
就像 if "Dummy": "True" 它应该执行 if 条件,而 if "dummy": false 它应该执行 else 条件,
我该怎么做?
我不确定我是否理解你的实际问题是什么。但这里有一些对您共享的代码的评论。
- 您上次打印的意图已关闭。
import Script2
可能不是您想要的。如果 Script2
中有函数,则在顶部导入它。然后调用函数即可。
- 在
if
你没有使用 ==
- 您正在用名为
json
的变量覆盖 json
模块
这是修改后的版本
import Script2
import json
with open('Data2.json', 'r') as handle:
json_out = json.load(handle)
profile_name = ['Test1']
data = [x for x in json_out if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['Dummy']=='True')
def getLogin():
if dummy:
Script2.myfunction()
else:
print("starting login process")
另一个小的改进是用生成器理解替换列表理解,因为你根本不需要列表:
data = next(x for x in json_out if x['profilename'] in profile_name)
email = (data['email'])
password = (data['password'])
payment = (data['payment'])
product = (data['product'])
dummy = (data['Dummy']=='True')
你好,我正在使用 python 并想将 JSON True 或 False 连接到我的代码,所以如果“dummy”:“True”做...否则:做... 我的 JSON
[
{
"profilename": "Test3",
"email": "email@outlook.com",
"password": "Password2*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"False"
},
{
"profilename": "Test1",
"email": "email@gmail.com",
"password": "Password1*",
"payment": "Paypal",
"product": "https://www.unkown.de/de/product/",
"Dummy":"True"
}
]
我的python代码:
profile_name = "Test3"
with open('Data2.json', 'r') as handle:
json = json.load(handle)
data = [x for x in json if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['dummy'])
def getLogin():
if dummy = "True": <----- This doesnt work, want to get this fixed
import Script2
else:
print("starting login process")
就像 if "Dummy": "True" 它应该执行 if 条件,而 if "dummy": false 它应该执行 else 条件, 我该怎么做?
我不确定我是否理解你的实际问题是什么。但这里有一些对您共享的代码的评论。
- 您上次打印的意图已关闭。
import Script2
可能不是您想要的。如果Script2
中有函数,则在顶部导入它。然后调用函数即可。- 在
if
你没有使用==
- 您正在用名为
json
的变量覆盖
json
模块
这是修改后的版本
import Script2
import json
with open('Data2.json', 'r') as handle:
json_out = json.load(handle)
profile_name = ['Test1']
data = [x for x in json_out if x['profilename'] in profile_name]
email = (data[0]['email'])
password = (data[0]['password'])
payment = (data[0]['payment'])
product = (data[0]['product'])
dummy = (data[0]['Dummy']=='True')
def getLogin():
if dummy:
Script2.myfunction()
else:
print("starting login process")
另一个小的改进是用生成器理解替换列表理解,因为你根本不需要列表:
data = next(x for x in json_out if x['profilename'] in profile_name)
email = (data['email'])
password = (data['password'])
payment = (data['payment'])
product = (data['product'])
dummy = (data['Dummy']=='True')