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

Categories

0 votes
660 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 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...