Skip to content Skip to sidebar Skip to footer

Removing Blank Spaces From A Csv File Without Creating A New File

I have blank spaces in a csv sheet that I want to get rid of it. After searching for hours I realized that this is the code for it: input = open('file.txt', 'wb') output = open('n

Solution 1:

You can first extract the valid rows and overwrite the file afterwards, provided your file is not too big and thus the rows can fit in the memory entirely

with open('file.txt', 'rb') as inp:
    valid_rows = [row for row in csv.reader(inp) if any(field.strip() for field in row)]

with open('file.txt', 'wb') as out:
    csv.writer(out).writerows(valid_rows)

Post a Comment for "Removing Blank Spaces From A Csv File Without Creating A New File"