简体   繁体   中英

Delete first rows of csv file in python

We want to delete the first 34 rows of our csv file, as it is useless text headers, which is to no use.

We are using following lines of code in Python 3:

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

Above should only 'delete' first line, but we imagine we could make a for loop with range(0, 35) around next(f).

Our csv data is in test.csv, and we have an empty csv file called temp.csv. The code above is supposed to skip first line of test.csv, and then copy the rest into temp.csv.

Unfortunately we receive this error:

Traceback (most recent call last):
  File "delete.py", line 2, in <module>
    next(f) # skip header line
  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 2625: invalid start byte

Why does this occur? And what is the pythonical way of deleting the first 34 rows in a csv file?

I know you want to skip rows using the with context manager when opening and reading a CSV file but I would suggest you to use an excellent library called pandas to read and skip row from the CSV file like below, also you can save that to another csv file from the df data frame very easily

import pandas as pd
#skiprows=34 will skip the first 34 lines and try to read from 35 line
df = pandas.read_csv('my_csv_file.csv', skiprows=34)
#print the data frame
df 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM