Path: csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.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; 'difference,': 0.07; 'executed': 0.07; 'csv': 0.09; 'dict': 0.09; 'modulo': 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; 'example:': 0.10; 'python': 0.10; 'assume': 0.11; 'file,': 0.15; 'skip:1 30': 0.15; 'columns': 0.16; 'paste.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'traceback.': 0.16; 'wrote:': 0.16; 'integer': 0.18; '>>>': 0.20; '2015': 0.20; 'sorry,': 0.22; 'am,': 0.23; 'code.': 0.23; '(most': 0.24; 'tim': 0.24; 'all.': 0.24; 'script': 0.25; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'error': 0.27; '(maybe': 0.29; 'behaviour': 0.29; 'cat': 0.29; 'chase': 0.29; 'division': 0.29; 'sure,': 0.29; 'print': 0.30; 'code': 0.30; 'guess': 0.31; 'post': 0.31; 'another': 0.32; 'especially': 0.32; 'knows': 0.32; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'list': 0.34; 'could': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'missing': 0.37; 'skip:z 10': 0.38; 'thank': 0.38; 'means': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'determine': 0.61; 'entire': 0.61; 'provide': 0.61; 'back': 0.62; 'you.': 0.64; 'here': 0.66; 'cut': 0.67; 'million': 0.74; 'chrisa': 0.84; "there'll": 0.84; 'whatsoever.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Finding Blank Columns in CSV Date: Tue, 06 Oct 2015 14:33:51 +0200 Organization: None References: <20151005090652.1c9faed7@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8428.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444134844 news.xs4all.nl 23727 [2001:888:2000:d::a6]:57787 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97441 Jaydip Chakrabarty wrote: > 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) Sorry, this is not the code you ran. I could guess what the missing parts might be, but it is easier for both sides if you provide a small script that actually can be executed and a small dataset that shows the behaviour you describe. Then post the session and especially the traceback. Example: $ cat my_data.csv 0 $ cat my_code.py print 1/int(open("my_data.csv").read()) $ python my_code.py Traceback (most recent call last): File "my_code.py", line 1, in print 1/int(open("my_data.csv").read()) ZeroDivisionError: integer division or modulo by zero Don't retype, use cut and paste. Thank you.