Path: csiph.com!goblin1!goblin.stu.neva.ru!uio.no!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: 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; 'cc:addr:python-list': 0.09; "'rb')": 0.09; 'subject:CSV': 0.09; 'python': 0.10; 'col': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration,': 0.16; 'row': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'code.': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'this.': 0.28; 'values': 0.28; 'print': 0.30; 'code': 0.30; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'there': 0.36; 'subject:: ': 0.37; 'data': 0.39; 'where': 0.40; 'here': 0.66; 'fin': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=I7Sh/5q5OgMQ4nCEg3MiMuGueQrIs5zEPEbPIHWKNxY=; b=yp8uAosGa0nOaLgAfQYEel7TJtnsT06s/31QLD2i4NdxxjnR9OvGOesCWaaYfVLfbo 2WZl5MANiC+yyaJ9u8zYYxUlHPwE3c1EV7t4z8FL6chqGZlaDCha8yoGaHbq9HsdhNmZ ZUGpExebdwomynjhRedClfE/Vo7pBPyOy5zQRKBigSQrO2mhqArlkzghW5E8MFxou3ZL GW2fUC4I3VxnFipVD+9QlCqdfqLSHsC3/SAMU/szaHL7lIerOTPKZdOqGCU8Tt8uiea9 c/iaCE0vBcoZas4zQPoOY/Uwu2g97IY1AaV9QfWtVL9acvVBQdnMfeBvzPScEkte1ZAh O1vQ== MIME-Version: 1.0 X-Received: by 10.107.162.196 with SMTP id l187mr26413287ioe.19.1444052931460; Mon, 05 Oct 2015 06:48:51 -0700 (PDT) In-Reply-To: References: Date: Tue, 6 Oct 2015 00:48:51 +1100 Subject: Re: Finding Blank Columns in CSV From: Chris Angelico To: Jaydip Chakrabarty Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444052940 news.xs4all.nl 23863 [2001:888:2000:d::a6]:36814 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97412 On Tue, Oct 6, 2015 at 12:29 AM, Jaydip Chakrabarty wrote: > 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. > You could do it with a single iteration, something like this: fn = "tmp1.csv" fin = open(fn, 'rb') rdr = csv.DictReader(fin, delimiter=',') # all the same down to here blanks = set(rdr.fieldnames) for row in data: blanks = {col for col in blanks if not row[col]} mt = [col for col in rdr.fieldnames if col not in blanks] print mt