Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing first line of Big CSV file?

How should I remove the first line of a big CSV file in python? I looked into the previous solutions in here, one was:

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'

the other 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:])

Which brings memory issue!

like image 917
Ha Hacker Avatar asked Jan 13 '15 08:01

Ha Hacker


1 Answers

Replace f.next() to 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)
like image 160
Hackaholic Avatar answered Nov 08 '22 17:11

Hackaholic