Reputation: 3
I have the following CSV data:
AAB BT 2 5 5
YUT HYT 89 52 3
JUI 10 2 3
HYIU 2 5 6
YHT JU 25 63 2
In 3rd row(and 4th row), first column(and 2nd column) elements are empty, so delete entire 3rd(and 4th) row. Similarly, the Entire 5th row is empty, also remove the entire 5th row. My desired output should be:
AAB BT 2 5 5
YUT HYT 89 52 3
YHT JU 25 63 2
I use the following code, but it is not deleating.
import csv
input(r'\\input.csv','r')
output=open(r'Outpur.csv','w',newline="")
writer= csv.writer(output)
for row in csv.reader(input):
if any(field.strip() for field in row):
writer.writerrow(row)
input.close()
output.close()
Upvotes: 0
Views: 908
Reputation: 552
#the following line reads the entire data from your csv file
#list contains list of every fields that are seperated by comma in your
csv file
#of every line
#
data_list=[[s.strip() for s in lines.split(',')] for lines in
open('c:/python34/yourcsv.csv')]
#now you can filter out any list inside your data list
#according to your choice
#such as lists whose length are less than it should be
# lets try it
filtered=[item for item in data_list if not(len(item)<5)]
#now you can overwrite the existing file or simply save it to new file
#and do whatever you want
for item in filtered:
print(item)
Upvotes: 0
Reputation: 1116
Firstly, please see the comment from @RomanPerekhrest on your original question. This answer assumes that you are not intending to delete your 6th row of data in your example.
It looks like your line of code:
if any(field.strip() for field in row):
is returning true if any field in the row of data contains an entry.
I suspect that what you want (if I understand your requirement correctly), is for the row of data to be kept if all of the fields in the row contain an entry. Therefore, try changing your code to use:
if all(field.strip() for field in row):
Also, check your debugger to ensure that your code is correctly tokenizing the row by splitting it at each comma in the CSV file.
If field.strip()
throws an exception (especially for empty fields), you might need to try employing a string length test instead. Something like:
if all(len(str(field).strip()) > 0 for field in row):
Upvotes: 1