Removing the first line of CSV file
For anyone else caught up this error:
AttributeError: '_io.TextIOWrapper' object has no attribute 'next' python
In Python3 a text file object doesn't have a next()
function.
So you can't call f.next()
.
Instead you should use f.readline()
as specified in this answer.
Or you can use the built-in next(f)
which @vrjr mentioned in the comment, and is shown in this answer.
This is what I do when I want to skip reading the first line of a CSV.
All that has to be done is call the next()
function of the CSV object, in this case - read
, and then the pointer to the reader will be on the next line.
import csv
try:
read = csv.reader(f)
read.next() # Skip the first 'title' row.
for r in read:
# Do something
finally:
# Close files and exit cleanly
f.close()
Hope this is pretty clean an simple for your purposes!
with open("test.csv",'r') as f:
with open("updated_test.csv",'w') as f1:
next(f) # skip header line
for line in f:
f1.write(line)