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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'resulting': 0.04; 'sufficient': 0.05; 'difference,': 0.07; 'executed': 0.07; 'seemed': 0.07; 'utf-8': 0.07; "'rb')": 0.09; 'codecs': 0.09; 'csv': 0.09; 'dict': 0.09; 'modulo': 0.09; 'nameerror:': 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; 'skip:f 30': 0.15; '"w",': 0.16; "'data'": 0.16; "'rb').read()": 0.16; 'columns': 0.16; 'fn)': 0.16; 'guessing': 0.16; 'nameerror': 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; 'found,': 0.18; 'integer': 0.18; 'script.': 0.18; '>>>': 0.20; '2015': 0.20; 'fix': 0.21; 'meant': 0.22; 'sorry,': 0.22; 'file.': 0.22; 'trying': 0.22; 'am,': 0.23; 'code.': 0.23; 'defined': 0.23; 'help.': 0.23; 'tried': 0.24; 'import': 0.24; '(most': 0.24; 'downloaded': 0.24; 'tim': 0.24; 'all.': 0.24; 'module': 0.25; 'script': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'skip:" 20': 0.26; 'compatible': 0.27; 'error': 0.27; 'right.': 0.27; '+0200,': 0.27; 'looks': 0.29; '(maybe': 0.29; 'behaviour': 0.29; 'cat': 0.29; 'chase': 0.29; 'division': 0.29; 'sure,': 0.29; 'convert': 0.29; 'asked': 0.29; 'print': 0.30; 'code': 0.30; "i'd": 0.31; 'guess': 0.31; 'post': 0.31; 'another': 0.32; 'especially': 0.32; 'knows': 0.32; 'getting': 0.33; 'run': 0.33; 'problem': 0.33; 'null': 0.33; 'traceback': 0.33; 'open': 0.33; 'tue,': 0.34; 'file': 0.34; 'list': 0.34; 'so,': 0.35; 'could': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'missing': 0.37; 'seem': 0.37; 'skip:z 10': 0.38; 'thank': 0.38; 'google': 0.39; '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; 'here.': 0.62; 'different': 0.63; 'you.': 0.64; '>>>>>': 0.66; 'here': 0.66; 'cut': 0.67; 'special': 0.73; 'million': 0.74; 'chrisa': 0.84; 'fin': 0.84; 'otten': 0.84; "there'll": 0.84; 'whatsoever.': 0.84; 'contacts': 0.97 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 20:20:40 +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: p57bd9a32.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: 169 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1444155660 news.xs4all.nl 23769 [2001:888:2000:d::a6]:52146 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97454 Jaydip Chakrabarty wrote: > On Tue, 06 Oct 2015 14:33:51 +0200, Peter Otten wrote: > >> 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. > > I downloaded gmail contacts in google csv format. There are so many > columns. So I was trying to create another csv with the required columns. > Now when I tried to open the gmail csv file with csv DictReader, it said > the file contained NULL characters. > So first I did - > > data = open(fn, 'rb').read() > fout = open(ofn, 'wb') > fout.write(data.replace('\x00', '')) > fout.close() > shutil.move(ofn, fn) > > Then I found, there were some special characters in the file. So, once > again I opened the file and did - > > data = open(fn, 'rb').read() > fout = open(ofn, 'wb') > fout.write(data.replace('\xff\xfe', '')) > fout.close() > shutil.move(ofn, fn) Uh this looks like the file is in UTF-16. Use import codecs fn = ... ofn = ... with codecs.open(fn, encoding="utf-16") as f: with codecs.open(ofn, "w", encoding="utf-8") as g: g.writelines(f) ... to convert it to UTF-8 which is compatible with the csv module of Python 2. > Now it seemed right. Only if all characters are encodable as iso-8859-1. > So I started to remove empty columns. > > fin = open(fn, 'rb') > fout = open(ofn, 'wb') > > rdr = csv.DictReader(fin, delimiter=',') > flds = rdr.fieldnames > 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, data[0].keys()) > dw.writeheader() > dw.writerows(data) > > fin.close() > fout.close() > > But, I am getting error at dw.writerows(data). I put the whole code here. > Please help. I really meant it when I asked you to post the code you actually ran, and the traceback it produces. When I fill in the blanks by guessing $ cat in.csv one,two,three foo,, bar,,baz $ cat remove_empty_colums.py import csv fn = "in.csv" ofn = "out.csv" fin = open(fn, 'rb') fout = open(ofn, 'wb') rdr = csv.DictReader(fin, delimiter=',') flds = rdr.fieldnames 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, data[0].keys()) dw.writeheader() dw.writerows(data) fin.close() fout.close() and then run the resulting script I get $ python remove_empty_colums.py Traceback (most recent call last): File "remove_empty_colums.py", line 18, in dw = csv.DictWriter(fout, data[0].keys()) NameError: name 'data' is not defined So this is my traceback, and while the NameError is trivial to fix (reopen the file or do a seek) but not sufficient to make the script do what you want it doesn't seem to be the problem you ran into. So you have a different script. I'd really like to see it, and the traceback it produces.