Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - 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:

'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,

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.

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. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First load you credentials into a dict with:

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:

{
    "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:

content = open("Creds.json").read()
content = content.replace("'", '"')
content = content.replace("}
", '},')
content = "{" + content + "}"
credentials = json.loads(content)

And here in production version

import json
credentials = json.loads("{" + open("Creds.json").read().replace("'", '"').replace("}
", '},') + "}")
while True:
    choice = input("Which account credentials do you want to use:" + "".join("
Press {0} for {0}".format(i + 1, credential) for i, credential in enumerate(credentials))+"
")
    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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...