How to Delete Blank Lines from CSV File

Delete blank rows from CSV?

Use the csv module:

import csv
...

with open(in_fnam, newline='') as in_file:
with open(out_fnam, 'w', newline='') as out_file:
writer = csv.writer(out_file)
for row in csv.reader(in_file):
if row:
writer.writerow(row)

If you also need to remove rows where all of the fields are empty, change the if row: line to:

if any(row):

And if you also want to treat fields that consist of only whitespace as empty you can replace it with:

if any(field.strip() for field in row):

Note that in Python 2.x and earlier, the csv module expected binary files, and so you'd need to open your files with e 'b' flag. In 3.x, doing this will result in an error.

Delete empty lines in csv file

To remove blank lines with sed:

sed -i '/^$/d' yourfile.csv

To remove lines consisting of a single $:

sed -i '/^$$/d' yourfile.csv

Most versions of sed support the -i switch; if yours does not you will need e.g. sed '/^$$/d' yourfile.csv > newfile.csv.

Removing blank lines with white space is more complicated. This usually works:

sed '/^ *$/d' yourfile.csv

If this is not sufficient, try checking also for tabs. For older sed's, this will work:

sed '/^[ X]*$/d' yourfile.csv

where X here a tab, entered via Control-V Tab.

Newer sed's will take a [ \t\r]* or \s* or [[:space:]]*, sometimes requiring a -E switch.

How to delete blank lines from CSV file?

You don't have a CSV file, so don't try to treat it as one. CSV specifically means a file consisting of rows of columns, where all rows have the same number of columns and the columns are delimited by a single character (usually a comma or a tab).

There is no need to delete the lines from your source file if all you needed was those first 4 values for your code either.

Just loop over the lines in the file, split each line by whitespace, and if the resulting list is not empty, take the last value:

numbers = []
with open(filename) as inf:
for line in inf:
data = line.split()
if data:
numbers.append(data[-1])

str.split() with no arguments splits on arbitrary length whitespace, including tabs, spaces and newlines, with any whitespace at the start and end ignored.

When a line is empty the line string will be '\n', and splitting such a line results in an empty list. If there is anything other than whitespace on a line, you get a list of strings for each non-whitespace section:

>>> '\n'.split()
[]
>>> 'P Inst Usage (Bytes): 13322\n'.split()
['P', 'Inst', 'Usage', '(Bytes):', '13322']

Because your file has the numbers you want at the end of each line, taking the last element (with [-1]) gets you all the numbers.

If you only need the top four such lines, test the len(numbers) result:

numbers = []
with open(filename) as inf:
for line in inf:
data = line.split()
if data:
numbers.append(data[-1])
if len(numbers) == 4:
break

The break stops the for loop, and when you exit the with statement, the file will be closed automatically.

If you do ever need to skip blank lines, just test if line.strip() is truthy; str.strip() uses the same arbitrary-width whitespace rules to remove whitespace from the start and end. Empty lines with just a newline separator end up as empty strings, which are considered false when used in a boolean test such as an if ...: condition:

if line.strip():
# line is not empty

You could then write that to a new file if you so wish, but that's not really needed here.

How to remove empty lines from CSV file in PHP?

If when you identify an empty row, you can then remove that row from the lines in the original array. Then just put all the remaining lines back together to the same file...

$lines = file("output.csv", FILE_SKIP_EMPTY_LINES );
$num_rows = count($lines);
foreach ($lines as $lineNo => $line) {
$csv = str_getcsv($line);
if (count(array_filter($csv)) == 0) {
unset($lines[$lineNo]);
}
}
file_put_contents("output.csv", $lines);

I've removed FILE_IGNORE_NEW_LINES so that the new lines are kept in the array.

how to remove blank lines from a csv file created using python

This code runs fine:

import csv
with open('input.csv', 'rb') as csvfile:

reader = csv.reader(csvfile, delimiter='|', quotechar='|')

writer1 = csv.writer(open('output.csv', 'wb'), delimiter = '|')

for row in reader:
a = row[0]
if (a=='D1'):
writer1.writerow(row)

Thanks to Pedru

Delete empty lines in CSV



Related Topics



Leave a reply



Submit