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


Groups > comp.lang.python > #40760

Re: Unhelpful traceback

Date 2013-03-07 08:10 -0500
From Dave Angel <davea@davea.name>
Subject Re: Unhelpful traceback
References <kh9c95$t68$1@dont-email.me>
Newsgroups comp.lang.python
Message-ID <mailman.3016.1362661824.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 03/07/2013 01:33 AM, John Nagle wrote:
> Here's a traceback that's not helping:
>

A bit more context would be helpful.  Starting with Python version.

> Traceback (most recent call last):
>    File "InfoCompaniesHouse.py", line 255, in <module>
>      main()
>    File "InfoCompaniesHouse.py", line 251, in main
>      loader.dofile(infile)                       # load this file
>    File "InfoCompaniesHouse.py", line 213, in dofile
>      self.dofilezip(infilename)                  # do ZIP file
>    File "InfoCompaniesHouse.py", line 198, in dofilezip
>      self.dofilecsv(infile, infd)        # as a CSV file
>    File "InfoCompaniesHouse.py", line 182, in dofilecsv
>      for fields in reader :                              # read entire
> CSV file
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in
> position 14: ordinal not in range(128)
>
> This is wierd, becuase "for fields in reader" isn't directly
> doing a decode. That's further down somewhere, and the backtrace
> didn't tell me where.
>
> The program is converting some .CSV files that come packaged in .ZIP
> files.  The files are big, so rather than expanding them, they're
> read directly from the ZIP files and processed through the ZIP
> and CSV modules.
>
> Here's the code that's causing the error above:


>
>          decoder = codecs.getreader('utf-8')
>          with decoder(infdraw,errors="replace") as infd :
>              with codecs.open(outfilename, encoding='utf-8', mode='w') as
> outfd :
>                  headerline = infd.readline()
>                  self.doheaderline(headerline)
>                  reader = csv.reader(infd, delimiter=',', quotechar='"')
>                  for fields in reader :
>                      pass
>
> Normally, the "pass" is a call to something that
> uses the data, but for test purposes, I put a "pass" in there.  It still
> fails.   With that "pass", nothing is ever written to the
> output file, and no "encoding" should be taking place.
>
> "infdraw" is a stream from the zip module, create like this:
>
>      with inzip.open(zipelt.filename,"r") as infd :

You probably need a 'rb' rather than 'r', since the file is not ASCII.

>          self.dofilecsv(infile, infd)
>
> This works for data records that are pure ASCII, but as soon as some
> non-ASCII character comes through, it fails.
>
> Where is the error being generated?  I'm not seeing any place
> where there's a conversion to ASCII.  Not even a print.
>
> 				John Nagle
>
>
>
>

If that isn't enough, then please give the whole context, such as where 
zipelt and filename came from.  And don't forget to specify Python 
version.  Version 3.x treats nonbinary files very differently than 2.x

-- 
DaveA

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


Thread

Unhelpful traceback John Nagle <nagle@animats.com> - 2013-03-06 22:33 -0800
  Re: Unhelpful traceback Andrew Berg <bahamutzero8825@gmail.com> - 2013-03-07 00:57 -0600
  Re: Unhelpful traceback Chris Rebert <clp2@rebertia.com> - 2013-03-06 23:04 -0800
  Re: Unhelpful traceback Dave Angel <davea@davea.name> - 2013-03-07 08:10 -0500
    Re: Unhelpful traceback John Nagle <nagle@animats.com> - 2013-03-07 10:42 -0800
      Re: Unhelpful traceback John Nagle <nagle@animats.com> - 2013-03-07 14:13 -0800
        Re: Unhelpful traceback Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 15:32 -0700
    Re: Unhelpful traceback John Nagle <nagle@animats.com> - 2013-03-07 13:23 -0800
      Re: Unhelpful traceback Dave Angel <davea@davea.name> - 2013-03-07 19:23 -0500

csiph-web