the last output running on infinite loop - Python

Viewed 83

My python code which picks links from row 1 and date from row 2 in order to run to_scrape function which requires both link and date, has the last output running infinitely. What is the best way to read and have the links+date (both columns of row) work/get data without getting the loop stuck

start = 0 
end = 2
csvfile = open('file.csv', 'r')
csvFileArray = []
for row in csv.reader(csvfile):
    csvFileArray.append(row[0])    

csvFileDate = []
for row in csv.reader(csvfile):
    csvFileDate.append(row[1])    

csvfile.close()

alums = csvFileArray[start:end]
alumdate = csvFileDate[start:end]


for i in alums:
    for j in alumdate:
    
        start+=1
        print(i)
        print(start)
        to_scrape(i,j)
2 Answers

Instead of the following (what you did):

for i in alums:
    for j in alumdate:
    
        start+=1
        print(i)
        print(start)
        to_scrape(i,j)

You should use the zip function, it can iterate through multiple sequences in parallel:

for i, j in zip(alums, alumdate):
        start+=1
        print(i)
        print(start)
        to_scrape(i,j)
for row in csv.reader(csvfile):
    csvFileArray.append(row[0])    

for row in csv.reader(csvfile):
    csvFileDate.append(row[1])  

These two for-loops are not-doing what you probably are thinking. They are re-opening the csvfile again and again continuously resulting in a infinite loop. Also note that you need to handle the two items in a row at the same time. Otherwise, you'll have to reset the file and start reading the file from the beginning again.

Here's the full code with bug fixes.

start = 0 
end = 2

csvFileArray = []
csvFileDate = []

#requires newline='' if you want to use it with csv.reader
with open('file.csv', newline='') as csvfile
    csv_reader = csv.reader(csvfile):
    for row in csv_reader:
        csvFileArray.append(row[0])
        csvFileDate.append(row[1])

alums = csvFileArray[start:end]
alumdate = csvFileDate[start:end]

for (i,j,start) in zip(alums, alumdate, range(1, len(alums)):
        print(start, i, j)
        to_scrape(i,j)
Related