[英]Reading credentials from one file into another
I have one py file where I want to call credentials of different accounts on the user choice, I have the below Creds.json
file with credentials as below:我有一个 py 文件,我想根据用户选择调用不同帐户的凭据,我有以下
Creds.json
文件,其凭据如下:
'Account1':{
"client_token" : "12345678",
"client_secret" : "9865432",
"access_token": "3459865"
}
'Account2':{
"client_token" : "33456787",
"client_secret" : "23456787",
"access_token": "98654378"
}
'Account3':{
"client_token" : "1234567",
"client_secret" : "87654378",
"access_token": "35627826"
}
I have a main.py
file where if the user inputs 1, 2 or 3 those credentials from the creds.json file will be used,我有一个
main.py
文件,如果用户输入 1、2 或 3,则将使用 creds.json 文件中的这些凭据,
choice = input("Which account credentials do you want to use: ")
print("Press 1 for Account 1")
print("Press 2 for Account 2")
print("Press 3 for Account 3")
when 1 is present I want the credentials under Account1 and when 2 is the choice credentials under Account2 should be imported here in the main.py
file for use.当 1 存在时,我想要 Account1 下的凭据,而当 2 是 Account2 下的选择凭据时,应在
main.py
文件中导入此处以供使用。
Please help me, how can I do it, I got confused when I tried it out, Do I need to change the Creds.json file to a py file or any other way, please provide a solution.请帮帮我,我该怎么做,我一试就糊涂了,我需要将Creds.json文件更改为py文件或其他方式,请提供解决方案。 Thanks in advance.
提前致谢。
First load you credentials into a dict with:首先将您的凭据加载到 dict 中:
import json
path = "Creds.json"
file = open(path)
content= file.read()
credentials = json.loads(content)
Then you have your input:然后你有你的输入:
choice = input("Which account credentials do you want to use: ")
print("Press 1 for Account 1")
print("Press 2 for Account 2")
print("Press 3 for Account 3")
And finally gather your crendential with:最后通过以下方式收集您的凭证:
credential = credentials["Account{0}".format(int(choice))]
You have to correct your file Creds.json
:您必须更正您的文件
Creds.json
:
{
"Account1":{
"client_token" : "12345678",
"client_secret" : "9865432",
"access_token": "3459865"
},
"Account2":{
"client_token" : "33456787",
"client_secret" : "23456787",
"access_token": "98654378"
},
"Account3":{
"client_token" : "1234567",
"client_secret" : "87654378",
"access_token": "35627826"
}
}
You can correct you Cred.json with this script:您可以使用以下脚本更正您的 Cred.json:
content = open("Creds.json").read()
content = content.replace("'", '"')
content = content.replace("}\n", '},')
content = "{" + content + "}"
credentials = json.loads(content)
And here in production version在生产版本中
import json
credentials = json.loads("{" + open("Creds.json").read().replace("'", '"').replace("}\n", '},') + "}")
while True:
choice = input("Which account credentials do you want to use:" + "".join("\nPress {0} for {0}".format(i + 1, credential) for i, credential in enumerate(credentials))+"\n")
key = "Account{0}".format(int(choice))
if key in credentials:
break
else:
print("Wrong answer, please retry (CRTC + C to exit)")
credential = credentials[key]
print(credential)
Here's a very quick and dirty example.这是一个非常快速和肮脏的例子。 First of all, I edited your json file to be a full JSON object:
首先,我将您的 json 文件编辑为完整的 JSON object:
Creds.json:
{
"Account1": {
"client_token": "12345678",
"client_secret": "9865432",
"access_token": "3459865"
},
"Account2": {
"client_token": "33456787",
"client_secret": "23456787",
"access_token": "98654378"
},
"Account3": {
"client_token": "1234567",
"client_secret": "87654378",
"access_token": "35627826"
}
}
Here is some python code for reading this file and selecting an account based on a selection.这是一些 python 代码,用于读取此文件并根据选择选择帐户。
main.py
import json
def main():
credFile = open('Creds.json')
creds = json.load(credFile)
print("Press 1 for Account 1")
print("Press 2 for Account 2")
print("Press 3 for Account 3")
choice = input("Which account credentials do you want to use: ")
chosenCreds = ""
if choice=="1":
chosenCreds=creds["Account1"]
elif choice=="2":
chosenCreds=creds["Account2"]
elif choice=="3":
chosenCreds=creds["Account3"]
else:
print("Invalid Selection!")
return
# Your code here using "Chosen Creds"...
print(chosenCreds)
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.