我正在嘗試修改我的 python 方法,以便它從單獨(dú)的 json 配置文件中讀取值。我有一個(gè)單獨(dú)的有效 json 文件,如下所示testtok.json: { "email" : "user@domain.com", "audience" : "http://someaudience.com", "jti" : "MYJTI1234", "name" : "John Smith", "humid" : "ABC1234" }我想將這些值傳遞給我的函數(shù):def tokengen(self, privatekey): with open('config/testtok.json', 'r') as config: data = json.load(config) try: """Simulate Token Manager creating a token""" email = config["email"] audience = config["audience"] jti = config["jti"] name = config["name"] humid = config["humid"] #email = "user@domain.com" #audience = "http://someaudience.com" #jti = "MYJTI1234" #name = "John Smith" #humid = "ABC1234" """Time Component""" timestamp = testdate() timestamp.now() issued = int(time.time()) expires_in=2400 expires = issued + expires_in additional_headers = { "alg": "RS256", "typ": "JWT" } payload = { "iss": email, "sub": email, "aud": audience, "iat": issued, "nbf": issued, "exp": expires, "jti": jti, "name": name, "humid": humid, "email": email }我看到此錯(cuò)誤,但不清楚如何解決此問(wèn)題。Traceback (most recent call last): File "testTokClass.py", line 25, in tokengen config["email"]TypeError: '_io.TextIOWrapper' object is not subscriptable有一個(gè)更好的方法嗎?理想情況下,我希望配置文件為 json。謝謝。
如何正確設(shè)置 json 配置文件,以便它可以用來(lái)替換 python 程序中的硬編碼值
狐的傳說(shuō)
2023-09-26 14:25:12