Finding Blank Columns in CSV

Random832 random832 at fastmail.com
Mon Oct 5 10:00:34 EDT 2015


On Mon, Oct 5, 2015, at 09:29, Jaydip Chakrabarty wrote:
> Hello,
> 
> I have a csv file like this.
> 
> Name,Surname,Age,Sex
> abc,def,,M
> ,ghi,,F
> jkl,mno,,
> pqr,,,F
> 
> I want to find out the blank columns, that is, fields where all the 
> values are blank. Here is my python code.
> 
> fn = "tmp1.csv"
> fin = open(fn, 'rb')
> rdr = csv.DictReader(fin, delimiter=',')
> data = list(rdr)
> flds = rdr.fieldnames
> fin.close()
> mt = []
> flag = 0
> for i in range(len(flds)):
>     for row in data:
>         if len(row[flds[i]]):
>             flag = 0
>             break
>         else:
>             flag = 1
>     if flag:
>         mt.append(flds[i])
>         flag = 0
> print mt
> 
> I need to know if there is better way to code this.

Well, for one thing, you should just be using something like "for fld in
flds" rather than range(len(...)).

There's also an opportunity to use list/generator comprehensions instead
of explicit loops - your whole loop could be written as:
mt = [fld for fld in flds if not any(row[fld] for row in data)]

It might be more efficient to iterate over rows first than columns,
depending on your data (if you have a large number of rows, consider
that you're reading them all into memory at the start) This would be
more complex code, though, and more difficult to write as a
comprehension, so if your data isn't ideal for it it might not be worth
doing.



More information about the Python-list mailing list