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


Groups > comp.lang.python > #97414

Re: Finding Blank Columns in CSV

Path csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <random832@fastmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'data:': 0.07; "'rb')": 0.09; 'csv': 0.09; 'iterate': 0.09; 'received:internal': 0.09; 'rows': 0.09; 'rows,': 0.09; 'subject:CSV': 0.09; 'python': 0.10; 'fld': 0.16; 'loops': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:messagingengine.com': 0.16; 'row': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'explicit': 0.22; 'code,': 0.23; 'code.': 0.23; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'this.': 0.28; 'values': 0.28; 'print': 0.30; 'code': 0.30; 'ideal': 0.32; 'though,': 0.32; 'file': 0.34; 'could': 0.35; 'something': 0.35; "isn't": 0.35; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'received:66': 0.38; 'data': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'hello,': 0.40; 'your': 0.60; 'header:Message-Id:1': 0.61; 'more': 0.63; 'here': 0.66; 'worth': 0.67; 'as:': 0.79; 'fin': 0.84; 'thing,': 0.93
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=vcqYPPP7qmhFE/JvlREw/cXW/NE=; b=SavUNN Swo/UrITObFZqKyc8B7znE4rkZkdTwYc2xl4dlGOi/aofIHvvcugbzWzmg9rf2of M56mopp6YAtfyXSbg08aunjZW/I71VhRJ4NQFQQLT+qrRaxGkR73Ee4vaWMqxfMk WEjxh7ORlkcVfKS+Qj/8oEZEUbWlrsJsXvvZQ=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=vcqYPPP7qmhFE/J vlREw/cXW/NE=; b=lRfaHn3QqQFvGeWFm+PEwE4dbbOFFM6ATvKLacbirjvMDct KUqOvKHm+e2Z600U3amOtLOWn9m5o2YrQD7tB4XIG73N59Ramt6qKXRbveYfzAil Dbt3nUJQT9vLnMn5SLm6kcV1WV8dfSVbIXBNcRFA8VYRwuI/p1/eDZgdr9qg=
X-Sasl-Enc PUw40KfDgxdnV+HL8TdDXNgF7sDECk1ALpbshLWlu9DT 1444053634
From Random832 <random832@fastmail.com>
To python-list@python.org
MIME-Version 1.0
Content-Transfer-Encoding 7bit
Content-Type text/plain
X-Mailer MessagingEngine.com Webmail Interface - ajax-b54601fa
Subject Re: Finding Blank Columns in CSV
Date Mon, 05 Oct 2015 10:00:34 -0400
In-Reply-To <muttuu$pc5$1@ger.gmane.org>
References <muttuu$pc5$1@ger.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.392.1444053638.28679.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1444053638 news.xs4all.nl 23752 [2001:888:2000:d::a6]:43576
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97414

Show key headers only | 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