Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #97453
| Path | csiph.com!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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 | <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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'difference,': 0.07; 'executed': 0.07; 'seemed': 0.07; "'rb')": 0.09; '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; 'skip:f 30': 0.15; "'rb').read()": 0.16; 'columns': 0.16; 'fn)': 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; 'thanks.': 0.18; '>>>': 0.20; '2015': 0.20; 'sorry,': 0.22; 'file.': 0.22; 'trying': 0.22; 'am,': 0.23; 'code.': 0.23; 'help.': 0.23; 'tried': 0.24; '(most': 0.24; 'downloaded': 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; 'right.': 0.27; '+0200,': 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; 'getting': 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; 'thanks': 0.37; 'received:org': 0.37; 'missing': 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; 'you.': 0.64; 'here': 0.66; 'cut': 0.67; 'special': 0.73; 'million': 0.74; 'received:116': 0.81; '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 | Jaydip Chakrabarty <chalao.adda@gmail.com> |
| Subject | Re: Finding Blank Columns in CSV |
| Date | Tue, 6 Oct 2015 17:23:31 +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> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | 116.193.139.135 |
| 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.431.1444152222.28679.python-list@python.org> (permalink) |
| Lines | 104 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1444152222 news.xs4all.nl 23726 [2001:888:2000:d::a6]:41240 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:97453 |
Show key headers only | View raw
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)
Now it seemed right. 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.
Thanks.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Finding Blank Columns in CSV Jaydip Chakrabarty <chalao.adda@gmail.com> - 2015-10-06 17:23 +0000
csiph-web