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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'yet.': 0.03; 'column': 0.07; 'data:': 0.07; 'cc:addr:python-list': 0.09; "'rb')": 0.09; 'subject:CSV': 0.09; 'bugs.': 0.16; 'col': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'row': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'assuming': 0.22; 'am,': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'end,': 0.29; 'print': 0.30; 'though,': 0.32; 'tue,': 0.34; 'received:google.com': 0.35; 'quite': 0.35; 'but': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'names': 0.38; 'does': 0.39; 'still': 0.40; 'field': 0.60; 'here': 0.66; 'chrisa': 0.84; 'fin': 0.84; 'premature': 0.84; 'proofread': 0.84; 'simple:': 0.84; 'to:none': 0.91 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:cc :content-type; bh=640wInNK8rblWyx2z9Y3lTqSPaL2MeBDx7FlJnCC4i4=; b=d2zmTSz/2YH7N/dObMHMTFlvuFstZnha9T+6SXVRKKe3Fwv3ANRQTlUBBH7+TMKDK8 wu/f3mS4K1TDFCtpJ7WwzvEBOZh+dsfmwbQFXOEltFdYes+X1VCo+DsVAHHWFk4KP3Km AebLA858Yjg/LRwMx5W+KFk9z5AkJhbGZlkGNk0Pql3DOgB8zgw2X6xTfYXiEDB6GKa4 XJWBUT7rFCSlfgii/wOaPqjRumctOUQ3TI5z3TziO8A8EDT4YD/rLiWQz5+kHjhkABKv myS3WVfNZrS2ORiTtiqXlQsCoT5LLi/+oWf9lvWoKTrSh9N2D3n8pD2RotkcncnDxkNu BqpA== MIME-Version: 1.0 X-Received: by 10.50.117.38 with SMTP id kb6mr5232894igb.92.1444053111906; Mon, 05 Oct 2015 06:51:51 -0700 (PDT) In-Reply-To: References: Date: Tue, 6 Oct 2015 00:51:51 +1100 Subject: Re: Finding Blank Columns in CSV From: Chris Angelico 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444053120 news.xs4all.nl 23823 [2001:888:2000:d::a6]:40308 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97413 On Tue, Oct 6, 2015 at 12:48 AM, Chris Angelico wrote: > 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 Oops, premature send - hadn't proofread it yet. 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 rdr: blanks = {col for col in blanks if not row[col]} mt = [col for col in rdr.fieldnames if col not in blanks] print mt Though I still haven't tested it, so there may be other bugs. Broadly speaking, though, what it does is quite simple: start by assuming that every column is nothing but blanks, and then any time you find a non-blank cell, remove it from the set of blanks. At the end, all field names not present in the set of blanks are non-blanks. ChrisA