简体   繁体   中英

how to delete some line in CSV file , using python

Please See Example.

i want to delete 1,2 lines in csv file, and use pandas

i try read_csv function in pandas library but pandas can not loading this CSV file

i can do delete line in Excel, but CSV file to many have.

so i want to delete line process in python.

Please let me know solution in python

Ex )

filename : xxxxxx
     time : xxxxx
     output1 output2 output3 output4 output5
        1       1       1      1       1
        1       1       1      1       1
        1       1       1      1       1

Pandas' read_csv function has a skiprows argument, which will let pandas know to skip either a fixed number of lines or a list of line numbers. In this case, you can use:

df = pd.read_csv('data.csv', skiprows=2)

Please see use in the documentation .

PS: This assumes the actual data is comma-separated. Otherwise, you can define a custom delimiter with sep argument.

Pandas is likely having a hard time parsing your CSV because of the filename and time data at the top of your file. If you delete those, and then use read_csv() in pandas again, it should not have any issues reading a tab or space delimited file.

EDIT: @Avidiotic's response is a better solution if you don't want to manually delete the lines, or you want to persist the filename and time data.

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