1

I have two lists named itemIds and itemRap, both containing exactly 1,890 items inside. My goal is to unload these lists into separate notepads. Here is the code segment:

#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

Whenever I run this segment, not all of the items in the lists end up in the notepads- they both are missing 300-400 items. Is python incapable of adding items to a notepad this fast? What is going on? Are there any potential solutions?

Here's my full code if you want to try it out for yourself:

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
E3x3
  • 21
  • 2
  • This is why we use [context managers](https://stackoverflow.com/a/6160082/1222951) to work with files. – Aran-Fey Jul 22 '18 at 07:21

0 Answers0