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


Groups > comp.lang.python > #68698

Re: csv read _csv.Error: line contains NULL byte

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mail@timgolden.me.uk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'skip:u 30': 0.07; '__name__': 0.09; 'function,': 0.09; 'try:': 0.09; 'def': 0.12; '"w")': 0.16; "'__main__':": 0.16; 'csv': 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'generator.': 0.16; 'help?': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'row)': 0.16; 'tjg': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'received:192.168.100': 0.24; 'define': 0.26; 'purposes': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'file': 0.32; 'open': 0.33; 'reader': 0.33; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'skip:f 40': 0.36; 'yield': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'email addr:gmail.com': 0.63; 'skip:n 10': 0.64; 'our': 0.64; 'fire': 0.65; 'from:addr:mail': 0.83; 'reading,': 0.84; 'subject:read': 0.84
Date Fri, 21 Mar 2014 14:59:01 +0000
From Tim Golden <mail@timgolden.me.uk>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0
MIME-Version 1.0
To python-list@python.org
Subject Re: csv read _csv.Error: line contains NULL byte
References <22aeefa3-cf82-457c-ab85-6f0366ff7b4e@googlegroups.com> <mailman.8354.1395409181.18130.python-list@python.org> <fefcec40-3bd9-4a94-9ae8-4f214fce2302@googlegroups.com>
In-Reply-To <fefcec40-3bd9-4a94-9ae8-4f214fce2302@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.8361.1395414009.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395414009 news.xs4all.nl 2845 [2001:888:2000:d::a6]:45856
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68698

Show key headers only | View raw


On 21/03/2014 14:46, chip9munk@gmail.com wrote:
> I am sorry I do not understand how to get to each row in this way.
> 
> Please could you explain also this:
> If I define this function, 
> how do I change my for loop to get each row?

Does this help?

<code>
#!python3
import csv

def unfussy_reader(csv_reader):
    while True:
        try:
            yield next(csv_reader)
        except csv.Error:
            # log the problem or whatever
            print("Problem with some row")
            continue

if __name__ == '__main__':
    #
    # Generate malformed csv file for
    # demonstration purposes
    #
    with open("temp.csv", "w") as fout:
        fout.write("abc,def\nghi\x00,klm\n123,456")

    #
    # Open the malformed file for reading, fire up a
    # conventional CSV reader over it, wrap that reader
    # in our "unfussy" generator and enumerate over that
    # generator.
    #
    with open("temp.csv") as fin:
        reader = unfussy_reader(csv.reader(fin))
        for n, row in enumerate(reader):
            print(n, "=>", row)


</code>


TJG

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


Thread

csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 06:29 -0700
  Re: csv read _csv.Error: line contains NULL byte Tim Golden <mail@timgolden.me.uk> - 2014-03-21 13:39 +0000
    Re: csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 07:46 -0700
      Re: csv read _csv.Error: line contains NULL byte chip9munk@gmail.com - 2014-03-21 07:59 -0700
      Re: csv read _csv.Error: line contains NULL byte Tim Golden <mail@timgolden.me.uk> - 2014-03-21 14:59 +0000
      Re: csv read _csv.Error: line contains NULL byte Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-21 15:15 +0000

csiph-web