Removing the first line of a large CSV file?

How to delete the first line of a large CSV file in python? Here I reviewed previous solutions:

with open("test.csv",'r') as f: with open("updated_test.csv",'w') as f1: f.next() # skip header line for line in f: f1.write(line) 

which gave me this error:

 f.next() # skip header line AttributeError: '_io.TextIOWrapper' object has no attribute 'next' 

another solution was:

 with open('file.txt', 'r') as fin: data = fin.read().splitlines(True) with open('file.txt', 'w') as fout: fout.writelines(data[1:]) 

What causes a memory problem!

+5
source share
2 answers

Replace f.next() with next(f)

 with open("test.csv",'r') as f, open("updated_test.csv",'w') as f1: next(f) # skip header line for line in f: f1.write(line) 
+9
source

use f .__ next __ () instead of f.next ()

here: https://docs.python.org/3/library/stdtypes.html#iterator. following

0
source

Source: https://habr.com/ru/post/1210961/


All Articles