私はプログラミングにかなり慣れておらず、Team Fortress 2プレーヤーからインベントリデータを取得し、steamidをキーとして、アイテムのリストを値として、インベントリアイテムを辞書に入れるプログラムを作成しました。
私が遭遇している問題は、辞書に約6000エントリが入力された後、プログラムがシステム上のRAMのすべてを本質的に吸い上げ、シャットダウンすることです。
辞書が大きくなりすぎると思いますが、同様の質問から読んだところによると、6000エントリの辞書は私のRAMの多くを占めるべきではありません。
私は他の解決策を検討してきましたが、コードにいくつかの具体的な例を使用できます。
import re, urllib.request, urllib.error, gzip, io, json, socket, sys
with open("index_to_name.json", "r", encoding=("utf-8")) as fp:
index_to_name=json.load(fp)
with open("index_to_quality.json", "r", encoding=("utf-8")) as fp:
index_to_quality=json.load(fp)
with open("index_to_name_no_the.json", "r", encoding=("utf-8")) as fp:
index_to_name_no_the=json.load(fp)
with open("steamprofiler.json", "r", encoding=("utf-8")) as fp:
steamprofiler=json.load(fp)
inventory=dict()
playerinventories=dict()
c=0
for steamid in steamprofiler:
emptyitems=[]
items=emptyitems
try:
url=urllib.request.urlopen("http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamid="+steamid+"&format=json")
inv=json.loads(url.read().decode("utf-8"))
url.close()
except (urllib.error.HTTPError, urllib.error.URLError, socket.error) as e:
c+=1
print("URL/HTTP error, continuing")
continue
try:
for r in inv["result"]["items"]:
inventory[r["id"]]=r["quality"], r["defindex"]
except KeyError:
c+=1
print(steamid, "didn't have an inventory")
continue
for key in inventory:
try:
if index_to_quality[str(inventory[key][0])]=="":
items.append(
index_to_quality[str(inventory[key][0])]
+""+
index_to_name[str(inventory[key][1])]
)
else:
items.append(
index_to_quality[str(inventory[key][0])]
+" "+
index_to_name_no_the[str(inventory[key][1])]
)
except KeyError:
print("Key error, uppdate def_to_index")
c+=1
continue
playerinventories[int(steamid)]=items
items=emptyitems
c+=1
print(c, "inventories fetched")
辞書の出現を維持しながらそれを行う他の方法を私は本当に知りません。これは、それが誰の在庫であるかを知りたいので非常に重要です。これについて不明な点がある場合は、そのように言ってください。説明しようと思います