Issue
i want to delete only first row (not the headers) of the csv in python I have tried many solutions with import csv or pandas but nothing have worked for me yet. all solutions either printed out the csv and didnt modify the original file.
And important i do not want to print out or skip/ignore the first line i want to delete it and save it to the original file not creating another file.
Thank you:)
Solution
FILENAME = 'test.csv'
DELETE_LINE_NUMBER = 1
with open(FILENAME) as f:
data = f.read().splitlines() # Read csv file
with open(FILENAME, 'w') as g:
g.write('\n'.join([data[:DELETE_LINE_NUMBER]] + data[DELETE_LINE_NUMBER+1:])) # Write to file
Original test.csv:
ID, Name
0, ABC
1, DEF
2, GHI
3, JKL
4, MNO
After run:
ID, Name
1, DEF
2, GHI
3, JKL
4, MNO
(deleted 0, ABC
)
Answered By - The Thonnu Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.