Very new to python and can’t understand why this isn’t working. I have a list of web addresses stored line by line in a text file. I want to store the first 10 in an array/list called bing, the next 10 in a list called yahoo, and the last 10 in a list called duckgo. I’m using the readlines function to read the data from the file into each array. The problem is nothing is being written to the lists. The count is incrementing like it should. Also, if I remove the loops altogether and just read the whole text file into one list it works perfectly. This leads me to believe that the loops are causing the problem. The code I am using is below. Would really appreciate some feedback.
count=0;
#Open the file
fo=open("results.txt","r")
#read into each array
while(count<30):
if(count<10):
bing = fo.readlines()
count+=1
print bing
print count
elif(count>=10 and count<=19):
yahoo = fo.readlines()
count+=1
print count
elif(count>=20 and count<=29):
duckgo = fo.readlines()
count+=1
print count
print bing
print yahoo
print duckgo
fo.close
You’re using
readlinesto read the files.readlinesreads all of the lines at once, so the very first time through your loop, you exhaust the entire file and store the result inbing. Then, every time through the loop, you overwritebing,yahoo, orduckgowith the (empty) result of the nextreadlinescall. So your lists all wind up being empty.There are lots of ways to fix this. Among other things, you should consider reading the file a line at a time, with
readline(no ‘s’). Or better yet, you could iterate over the file, line by line, simply by using aforloop:To keep the structure of your current code you could use
enumerate:But frankly I think you should ditch your current system. A much simpler way would be to use
readlineswithout a loop, and slice the resulting list!There are many other ways to do this, and some might be better, but none are simpler!