Python - Loadtext with specific lines for huge file
Question: I have to get specific lines in a huge text file. Until now i try as below. My
aim is to extract columns for a specific iteration, here each 500 lines. But
by proceeding with the "readlines", sometimes i get some crashes because of
the size of the file (until 4Gb). So i would like to find an other way in
order to avoid problems...

    
    
    with open('/test.txt') as f:
        text = f.readlines()
    
    
    A = ""    
    for i in text[3000:3500]:
      A+=i
    
    B=A.splitlines()
    listed = []
    for i in range(len(B)):
      C=B[i][3:47].split(" ")
    
      while True:
        try:
          C.remove("")
        except ValueError:
          break
      listed.append(C)
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    #print listed
    x = np.array(listed, dtype=float)
    y = x.astype(np.float)
    
    plt.plot(y[:,1]);plt.ylim(0,5);plt.show()
    

This post follows a former
[question](http://stackoverflow.com/questions/32559304/python-loadtext-for-
specific-number-of-lines).


Answer: If I understand correctly you want to get the lines 3000 to 3500. You can do
this like such:

    
    
    import itertools
    with open('test.txt') as f:
        lines = list(itertools.islice(f, 3000, 3500))
    

