我正在编写一个程序,该程序会在api密钥超时时更新它们,因此我有一个json文件,该文件可跟踪所有api密钥以及它们是否处于冷却状态,但是我正在努力寻找一种更新“ cooldown”的方法”,而不是仅编辑api key属性中包含的数据。
预先感谢您的帮助
import json
import io
import time
import re
def json_Data_Pull():
file = open('api_Key.json', 'r')
data = json.load(file)
return data
#Each api key is assigned a number and when this function is called the specific
#number given in parameters is called, if that number isn't on cooldown, it will
#be returned, otherwise the function will at +1 to that and find another key
def new_Key_Pull(number):
data = json_Data_Pull()
arr = []
try:
loads = json.loads(data)
js = json.dumps(loads['api' + number])
cooldown = re.search(r'("cooldown": "(.*?)")', js, )
final = re.sub(r'"cooldown": "|"', '' , cooldown.group())
arr.insert(1, final)
api = re.search(r'("api(.*)": "(.*?)"})', js, )
final2 = re.sub(r'"api(.*)": "|"|}', '', api.group())
arr.insert(2, final2)
return arr
except(TypeError):
pass
#This function sets the current state of an API key to false, and then waits for 62 seconds
#and resetts it to true in order to create a cooldown on each key so that api timeouts
#don't occurr
def cooldown_Key(api):
loads = json.loads(json_Data_Pull())
jsd = json.dump(loads)
Json文件:
"""{
"api1":{
"api1": "Num 1",
"cooldown": "False"
},
"api2":{
"api2": "Num 2",
"cooldown": "False"
},
"api3":{
"api3": "Num 3",
"cooldown": "True"
}
} """
答案 0 :(得分:0)
尝试一下:
data = """{
"api1":{
"api1": "Num 1",
"cooldown": "False"
},
"api2":{
"api2": "Num 2",
"cooldown": "False"
},
"api3":{
"api3": "Num 3",
"cooldown": "True"
}
} """
data = json.loads(data)
# example: modify "api1" cooldown to "True"
data["api1"]["cooldown"] = "True"
print(data)