I use Python3 and I want to read and print the first Nth rows of a .txt (the file is 40GB+ so I can't open it because of RAM limitations). I just want to understand the file structure (columns, variable names, separators,...). With the below code, Python gives me []
as an output (instead of the printed lines I'd want):
from itertools import islice
with open("filename.txt") as myfile:
head = list(islice(myfile, 1, 25))
print(head)
I also tried adding 'r'
next to the file name, but did not succeed. I only want to be able to read the first Nth rows (be it 25 rows, 5,10, or 15, this I don't care).
Hi, responses below addressed me to the .txt file (and not python code). I completely changed my approach and try to read initial 100 rows using pd.read_csv as follows:
dfcontact2 = pd.read_csv('filename.txt', sep='|', names=['col1'], nrows=100)
dfcontact2.head(5)
The code outputs:
where row 0 are variable names. I do not see any '\n' character at the end of each row, so I guess the file is not structured in lines, but why is then the output offered in rows? What am I missing?
Thanks a lot for your time. Best,