Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete first rows of csv file in python

Tags:

python

csv

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 trying to do so by running the 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). Although that doesn't seem like the "pythonical" way of solving this.

Our data is in test.csv, and we have an empty csv file called temp.csv. The code described 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?

like image 820
Buster3650 Avatar asked Dec 22 '22 19:12

Buster3650


1 Answers

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 = pd.read_csv('my_csv_file.csv', skiprows=34)
# print the data frame
df 
like image 68
Always Sunny Avatar answered Dec 31 '22 06:12

Always Sunny