20

This is my code i am able to print each line but when blank line appears it prints ; because of CSV file format, so i want to skip when blank line appears

import csv
import time

ifile = open ("C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv", "rb")
for line in csv.reader(ifile): 
    if not line: 
        empty_lines += 1 
        continue
    print line
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
ketan
  • 263
  • 2
  • 4
  • 12
  • Why do you open your file in binary mode? Regardless of that, you should iterate over your lines in `data` variable. That assignation to `empty_lines` is incorrect without declaring it first and you have a typo in the `for` with an extra closing parentheses. – Birei Sep 19 '13 at 09:22
  • That code does not print anything because the file is consumed into `data` before the `for` loop. – Janne Karila Sep 19 '13 at 10:26
  • Janne yes you are right it does not print anything, but actually i have tried to print individual cell thats why i have stored in an array. – ketan Sep 19 '13 at 11:15
  • 2
    @Birei: "Why do you open your file in binary mode?" Because that's the right portable way to open files to pass to `csv.reader` in Python 2, as mentioned in the [docs](http://docs.python.org/2/library/csv.html#csv.reader). – DSM Sep 19 '13 at 12:21
  • You may use filter or generator approach similar to this answer: http://stackoverflow.com/a/14158869/1317713 I recommend re-factoring some of the logic into `def is_empty_line(line): ...` for readability, say if you want to skip a line containing all white-space. It can also be a good idea to skip both comments and empty lines - more reason to re-factor into separate functions. – Leonid Jun 02 '15 at 02:54

8 Answers8

15

If you want to skip all whitespace lines, you should use this test: ' '.isspace().

Since you may want to do something more complicated than just printing the non-blank lines to the console(no need to use CSV module for that), here is an example that involves a DictReader:

#!/usr/bin/env python
# Tested with Python 2.7

# I prefer this style of importing - hides the csv module
# in case you do from this_file.py import * inside of __init__.py
import csv as _csv


# Real comments are more complicated ...
def is_comment(line):
    return line.startswith('#')


# Kind of sily wrapper
def is_whitespace(line):
    return line.isspace()


def iter_filtered(in_file, *filters):
    for line in in_file:
        if not any(fltr(line) for fltr in filters):
            yield line


# A dis-advantage of this approach is that it requires storing rows in RAM
# However, the largest CSV files I worked with were all under 100 Mb
def read_and_filter_csv(csv_path, *filters):
    with open(csv_path, 'rb') as fin:
        iter_clean_lines = iter_filtered(fin, *filters)
        reader = _csv.DictReader(iter_clean_lines, delimiter=';')
        return [row for row in reader]


# Stores all processed lines in RAM
def main_v1(csv_path):
    for row in read_and_filter_csv(csv_path, is_comment, is_whitespace):
        print(row)  # Or do something else with it


# Simpler, less refactored version, does not use with
def main_v2(csv_path):
    try:
        fin = open(csv_path, 'rb')
        reader = _csv.DictReader((line for line in fin if not
                                  line.startswith('#') and not line.isspace()),
                                  delimiter=';')
        for row in reader:
            print(row)  # Or do something else with it
    finally:
        fin.close()


if __name__ == '__main__':
    csv_path = "C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv"
    main_v1(csv_path)
    print('\n'*3)
    main_v2(csv_path)
Leonid
  • 622
  • 3
  • 9
  • 22
  • 3
    Beware: This method is likely to clobber files having newlines inside quoted fields. In this case the number of lines in the file is not comparable to the number of delimited records. See [CSV module docs](https://docs.python.org/3/library/csv.html#csv.reader), with explanation at the [bottom of the page](https://docs.python.org/3/library/csv.html#id3). – ASL Mar 01 '17 at 15:12
12

Instead of

if not line:

This should work:

if not ''.join(line).strip():
seddonym
  • 16,304
  • 6
  • 66
  • 71
5

my suggestion would be to just use the csv reader who can delimite the file into rows. Like this you can just check whether the row is empty and if so just continue.

import csv

with open('some.csv', 'r') as csvfile:

    # the delimiter depends on how your CSV seperates values
    csvReader = csv.reader(csvfile, delimiter = '\t')

    for row in csvReader:
        # check if row is empty
        if not (row):    
            continue
Inga
  • 51
  • 1
  • 1
5

You can always check for the number of comma separated values. It seems to be much more productive and efficient.

When reading the lines iteratively, as these are a list of comma separated values you would be getting a list object. So if there is no element (blank link), then we can make it skip.

        with open(filename) as csv_file:
          csv_reader = csv.reader(csv_file, delimiter=",")
          for row in csv_reader:
            if len(row) == 0:
                continue
Pasan Chamikara
  • 715
  • 9
  • 21
1

You can strip leading and trailing whitespace, and if the length is zero after that the line is empty.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1
import csv

with open('userlist.csv') as f:

    reader = csv.reader(f)
    user_header = next(reader)       # Add this line if there the header is

    user_list = []                   # Create a  new user list for input
    for row in reader:
        if any(row):                 # Pick up the non-blank row of list
            print (row)              # Just for verification
            user_list.append(row)    # Compose all the rest data into the list
Revolver_Ocelot
  • 8,609
  • 3
  • 30
  • 48
Tony Ho
  • 21
  • 1
1

This example just prints the data in array form while skipping the empty lines:

import csv

file = open("data.csv", "r")
data = csv.reader(file)

for line in data:
    if line: print line

file.close()

I find it much clearer than the other provided examples.

drws
  • 111
  • 2
0
import csv
ifile=csv.reader(open('C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv', 'rb'),delimiter=';')
for line in ifile:
    if set(line).pop()=='':
        pass
    else:
        for cell_value in line:
            print cell_value
  • 1
    Not pythonic. Apart from that: `set("\n").pop() == '\n'`, so a blank line with a newline at the end will still be printed. – Johannes Charra Sep 19 '13 at 09:50
  • @srinu j thank dude, i can skip blank line but after that how an i access individual cell value, i know if i can store file in 'list' then i can access but problem is then i get blank line also. – ketan Sep 19 '13 at 11:32
  • when u iterating line in file it will give a list you can access line[0] to get first value of the list. – Srinivasreddy Jakkireddy Sep 20 '13 at 05:55
  • @Srinu J, i did but get individual full Column output, i want to access individual cell value when i iterate line. – ketan Sep 20 '13 at 07:38
  • iterate line print the cell values. – Srinivasreddy Jakkireddy Sep 20 '13 at 12:58
  • Instead of `if set(line).pop()=='': pass; else:` I recommend this: `if set(line).pop()=='': continue`. However, you can filter out the lines before they go into the csv reader constructor, because said reader simply iterates over the object that was passed in to it, so you can use `str.isspace()` in combination with `itertools.ifilter` to filter out the blank lines in a lazy fashion. – Leonid Jun 02 '15 at 03:48
  • Alternatively, you can use a lazy generator expression, such as: ` `try:; fin = open('C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv', 'rb'); reader = csv.reader((line for line in fin if not line.isspace())),delimiter=';'); # do something with reader; finally: fin.close()`. – Leonid Jun 02 '15 at 03:57
  • Please add some explanatory to your answer. – kenorb Jun 03 '15 at 10:19