Having a damn stroke over here.
I'm not a coder by any stretch, I'm just one of these people who want custom programs to make life easier so I spend way too long trying to make them. I'm also mentally challenged enough so that my head hurts trying to comprehend this crap.
Not gonna go into too many specifics on the background. the myquery thing runs a script that pulls from my work database. Everything gets put into a list. From there it's supposed to dump everything into a Google Sheet, one line at a time. Worked great when it was just two variables I had to worry about and I could use something binary like a dictionary. Well, now I got more; 4 categories, each need to be on their own line.
I just need to split up the list (rawskudata) into a bunch of smaller lists (componant) assigned to a dictionary (skuandimages). The problem is on this line:
skuandimages[c_list] = [rawskudata[int(c_sku)], rawskudata[int(c_img_url)], rawskudata[int(c_name)], rawskudata[int(c_quantity)]]
and I get IndexError: list index out of range.
based on me staring at it for literally two straight hours and making every google search result for "indexerror" purple, this SHOULD work. the list it's pulling from DOES have the index number. i checked with all sorts of print statements. why. why is it not. i want to die
mycursor = mydb.cursor()
skuandimages = {
}
myquery2 = #insert top secret query here
mycursor.execute(myquery2)
rawskudata = []
c_tag = 0
c_sku = 0
c_img_url = 1
c_name = 2
c_quantity = 3
print(mycursor)
for xy in mycursor:
for yx in range(2,6):
rawskudata.append(str(xy[yx]))
print(rawskudata)
for z in range(0,len(rawskudata)):
#skuandimages[str(x[2]) "-" str(x[3])] = x[4]
c_list = "componant" str(c_tag)
skuandimages[c_list] = [rawskudata[int(c_sku)], rawskudata[int(c_img_url)], rawskudata[int(c_name)], rawskudata[int(c_quantity)]]
#skuandimages[c_list] = [x for x]
c_sku = c_quantity 1
c_img_url = c_quantity 2
c_name = c_quantity 3
c_quantity = c_quantity 4
c_tag = 1
The print(rawskudata) returns this (data altered for privacy stuff):
['222001-1', 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Garden_strawberry_(Fragaria_×_ananassa)_single.jpg/440px-Garden_strawberry_(Fragaria_×_ananassa)_single.jpg', 'Strawberry', '1', '222014-1', 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Ripe,_ripening,_and_green_blackberries.jpg/440px-Ripe,_ripening,_and_green_blackberries.jpg', 'Blackberry', '1', '222053-1', 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Oranges_-_whole-halved-segment.jpg/440px-Oranges_-_whole-halved-segment.jpg', 'Oranges', '1', '222123-1', 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Autumn_Red_peaches.jpg/440px-Autumn_Red_peaches.jpg', 'Peaches', '1', '222203-1', 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Pears.jpg/440px-Pears.jpg', 'Pears', '1']
CodePudding user response:
You're grabbing data from rawskudata
4 at a time, so you want to loop over 1/4 the number of items in rawskudata
for unused in range(0, int(len(rawskudata)/4)):
But there is an easier way to go about this. You can replace everything that happens after the line mycursor.execute(myquery2)
with:
for xy in mycursor:
skuandimages["componant" str(c_tag)] = [xy[2], xy[3], xy[4], xy[5]]
c_tag = c_tag 1