Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97478

Re: Finding Blank Columns in CSV

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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:plane.gmane.org': 0.16; 'traceback.': 0.16; 'wrote:': 0.16; 'found,': 0.18; 'integer': 0.18; 'script.': 0.18; 'thanks.': 0.18; '>>>': 0.20; '2015': 0.20; 'fix': 0.21; 'meant': 0.22; 'so.': 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; '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; 'received:115.187': 0.84; "there'll": 0.84; 'whatsoever.': 0.84; 'received:115': 0.93; 'contacts': 0.97
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Jaydip Chakrabarty <chalao.adda@gmail.com>
Subject Re: Finding Blank Columns in CSV
Date Wed, 7 Oct 2015 11:56:18 +0000 (UTC)
References <muttuu$pc5$1@ger.gmane.org> <CAPTjJmrdE-n=VazddU7PZS6E=rbBf1XVoz_g2ArxK6454D5qxw@mail.gmail.com> <CAPTjJmqYwD0=KNLaHu_w_sOjUoacQRXFhb4Zxdv6n++5A-Y8iQ@mail.gmail.com> <20151005090652.1c9faed7@bigbox.christie.dr> <CAPTjJmpv1OOEomjjaqQ5qiGQDHpxAR4RpxLxn1Rt+j=TQBz4Yg@mail.gmail.com> <mv0b1h$5rd$1@ger.gmane.org> <mv0f3g$8pd$1@ger.gmane.org> <mv102j$oil$1@ger.gmane.org> <mv13dp$igf$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 115.187.47.246
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.452.1444219001.28679.python-list@python.org> (permalink)
Lines 169
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1444219001 news.xs4all.nl 23721 [2001:888:2000:d::a6]:42668
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97478

Show key headers only | View raw


On Tue, 06 Oct 2015 20:20:40 +0200, Peter Otten wrote:

> 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
>>>>> <python.list@tim.thechases.com> 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 <module>
>>>     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 <module>
>     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.

Yes, that is so.
Thank you all for the help.

Thanks.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Finding Blank Columns in CSV Jaydip Chakrabarty <chalao.adda@gmail.com> - 2015-10-07 11:56 +0000

csiph-web