Implementing large data set causing "string index out of range" error in Python?
Question: I am trying to implement the following code:

    
    
    with open('datapath') as f:
       k_mers = f.read()
    
    k_mers_parsed = []
    
    for i in k_mers.split('\n'):
       k_mers_parsed.append(i)
    genome = k_mers_parsed[0]
    for i in range(1,len(k_mers_parsed)):
       genome = genome + k_mers_parsed[i][-1]
    
    with open('filepath', 'w') as out:
    out.write(genome)
    

Reading a file that has about 4 lines of code works perfectly and I generate
the solution set needed in a quick manner.

However, whenever I try to import a large scale dataset, one with say, 1000
lines, I get this error:

    
    
    genome = genome + k_mers_parsed[i][-1]
    IndexError: string index out of range
    

What is the root cause of this and how can I remedy this?


Answer: You likely have an empty line in the file. Also try this:

    
    
    with open('datapath') as f:
        genome = [l[-1] for l in f if l]
    

