Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97414

Re: Finding Blank Columns in CSV

From Random832 <random832@fastmail.com>
Subject Re: Finding Blank Columns in CSV
Date 2015-10-05 10:00 -0400
References <muttuu$pc5$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.392.1444053638.28679.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Finding Blank Columns in CSV Random832 <random832@fastmail.com> - 2015-10-05 10:00 -0400

csiph-web