我对编程非常陌生,并制作了一个程序,用于从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 …Run Code Online (Sandbox Code Playgroud)