Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'difference,': 0.07; 'so?': 0.07; 'csv': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:CSV': 0.09; 'assume': 0.11; 'file,': 0.15; 'columns': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.16; 'thanks.': 0.18; '2015': 0.20; 'am,': 0.23; 'code.': 0.23; 'help.': 0.23; 'tim': 0.24; 'all.': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'chris': 0.26; 'error': 0.27; '(maybe': 0.29; 'chase': 0.29; 'sure,': 0.29; 'another': 0.32; 'knows': 0.32; 'tue,': 0.34; 'file': 0.34; 'list': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'difference': 0.38; 'means': 0.39; 'why': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'determine': 0.61; 'entire': 0.61; 'back': 0.62; 'between': 0.65; 'here': 0.66; 'million': 0.74; 'received:116': 0.81; 'chrisa': 0.84; "there'll": 0.84; 'whatsoever.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Jaydip Chakrabarty Subject: Re: Finding Blank Columns in CSV Date: Tue, 6 Oct 2015 11:24:33 +0000 (UTC) References: <20151005090652.1c9faed7@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 116.193.139.6 User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444130689 news.xs4all.nl 23760 [2001:888:2000:d::a6]:46226 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97440 On Tue, 06 Oct 2015 01:34:17 +1100, Chris Angelico wrote: > On Tue, Oct 6, 2015 at 1:06 AM, Tim Chase > wrote: >> That way, if you determine by line 3 that your million-row CSV file has >> no blank columns, you can get away with not processing all million >> rows. > > Sure, although that effectively means the entire job is moot. I kinda > assume that the OP knows that there are some blank columns (maybe lots > of them). The extra check is unnecessary unless it's actually plausible > that there'll be no blanks whatsoever. > > Incidentally, you have an ordered_headers list which is the blank > columns in order; I think the OP was looking for a list of the > _non_blank columns. But that's a trivial difference, easy to tweak. > > ChrisA Thanks to you all. I got it this far. But while writing back to another csv file, I got this error - "ValueError: dict contains fields not in fieldnames: None". Here is my code. rdr = csv.DictReader(fin, delimiter=',') header_set = set(rdr.fieldnames) for r in rdr: header_set = set(h for h in header_set if not r[h]) if not header_set: break for r in rdr: data = list(r[i] for i in header_set) dw = csv.DictWriter(fout, header_set) dw.writeheader() dw.writerows(data) Also, there is difference between len(header_set) and len(data[0].keys). Why is so? Thanks again for all your help. Thanks.