我有两个名为itemIds和itemRap的列表,它们都包含正好包含1,890个项目。我的目标是将这些列表卸载到单独的记事本中。这是代码段:
#unloading itemIds and itemRap lists into files
x = 0
for x in range(len(itemIds)):
itemId = (str(itemIds[x])+"\n" )
itemPrice = (str(itemRap[x])+"\n" )
itemIdFile.write(itemId)
itemRapFile.write(itemPrice)
x = x + 1
每当我运行此段时,并不是列表中的所有项目最终都出现在记事本中-它们都缺少300-400项。 python无法如此快速地将项目添加到记事本吗?到底是怎么回事?有潜在的解决方案吗?
如果您想自己尝试一下,这是我的完整代码:
import json, urllib
import os
itemRap = []
itemIds = []
pageNumber = 1
#initiating to make first while loop run
url = "https://search.roblox.com/catalog/json?Category=2&PageNumber=" + str(pageNumber)
response = urllib.urlopen(url)
data=json.loads(response.read())
while data:
#gets data for 1 page of collectibles
#puts data into lists based on Id and RAP
counter = 0
while counter != len(data):
if data[counter]['BestPrice'] != '':
itemIds.append(data[counter]['AssetId'])
itemRap.append(data[counter]['BestPrice'])
counter = counter + 1
pageNumber = pageNumber + 1
url = "https://search.roblox.com/catalog/json?Category=2&PageNumber=" + str(pageNumber)
response = urllib.urlopen(url)
data=json.loads(response.read())
print(len(itemIds))
print(len(itemRap))
#clears important files- change to graph data later?
itemIdFile = open("ItemIds.txt","a+")
itemRapFile = open("ItemRap.txt","a+")
itemIdFile.truncate(0)
itemRapFile.truncate(0)
#unloading itemIds and itemRap lists into files
x = 0
for x in range(len(itemIds)):
itemId = (str(itemIds[x])+"\n" )
itemPrice = (str(itemRap[x])+"\n" )
itemIdFile.write(itemId)
itemRapFile.write(itemPrice)
x = x + 1