[
{"923390702359048212": 5},
{"462291477964259329": 1},
{"803390252265242634": 3},
{"824114065445486592": 2},
{"832041337968263178": 4}
]
這是我隨機生成的用戶 ID 串列和每個 ID 的一些示例編號。在這種情況下,我們可以將其稱為視頻游戲中一個賽季的進球數。當我嘗試更新比賽結束時的進球數時,我必須采用一種迂回的方式,首先獲取服務器中的所有成員 ID,然后將其與具有以下代碼的資料進行比較。
amount = 0
for member in server:
if member.id != server.member.id:
amount = amount 1
else:
print(amount)
print(str(member.id), str(server.member.id))
print(jsondata[amount][str(server.member.id)])
break
jsondata[amount][str(server.member.id) = jsondata[amount][str(server.member.id)] 1
有沒有更好的方法來做我正在做的事情?我知道我最終會遇到問題,因為我不會在 json 上列出成員,直到他們得分,而且我覺得我在程式上浪費了大量時間,因為它檢查了很多成員(我編輯了我的真實串列并制作了這個串列以使其更容易,因為沒有人需要看到 50 多個條目才能獲得這個想法)。在這方面,我仍然是初學者,所以請用簡單的術語回答或留下指向我可以學習更復雜想法的地方的鏈接。謝謝
def goalscored():
amount = 0
for member in server:
if member.id != server.member.id:
amount = amount 1
else:
print(amount)
break
with open('Goals.json','w') as f:
jsondata = json.load(f)
jsondata[amount][str(server.member.id) = jsondata[amount][str(server.member.id)] 1
json.dump(jsondata,f)
def firstgoal(server.member.id):
with open('Goals.json','w') as f:
jsondata = json.load(f)
amount = 0
goalscored = 1
totalmembers = server.members.amount
for member in server:
if membed.id !=server.member.id:
amount = amount 1
if amount == totalmembers:
NewScore = {user.id:goalscored}
json.dump(NewScore,f)
我正在使用的代碼
uj5u.com熱心網友回復:
不知道為什么你不能更早地讓它作業,但將它存盤為 dict 會容易得多。
# file.json
{
"923390702359048212": 5,
"462291477964259329": 1,
"803390252265242634": 3,
"824114065445486592": 2,
"832041337968263178": 4
}
def goalscored():
with open("Goals.json", "r") as f:
jsondata = json.load(f)
# Make sure your id is not int but str
if server.member.id not in jsondata: # first goal case
jsondata[server.member.id] = 0
jsondata[server.member.id] = 1
with open("Goals.json", "w") as f:
json.dump(jsondata, f)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394287.html
上一篇:轉義jq中的正則運算式特殊字符
