[Tutor] why is unicode converted file double spaced?

Kent Johnson kent37 at tds.net
Tue Apr 7 23:54:42 CEST 2009

On Tue, Apr 7, 2009 at 2:59 PM, Pirritano, Matthew <MPirritano at ochca.com> wrote:

> Traceback (most recent call last):
>   File "C:\Projects\unicode_convert.py", line 8, in <module>
>     outp.write(outLine.strip()+'\n')
> UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 640-641: ordinal not in range(128)
> Should I be worried about this. And where does this message indicate that
> the error is. And what is the error?

The error is that your source file contains characters that are not in
the Ascii set; in particular a character with value 128 was found at
location 640 in some unknown line.

Yes, you should be worried; the processing aborted at that point.

The error happens during an implicit conversion from unicode to ascii
within the write() call. What you have is equivalent to
  outp.write(outline.strip().encode('ascii') + '\n')

You can the encoder to ignore errors or replace them with ? by adding
the parameter
  errors = 'ignore'
  errors = 'replace'
to the encode() call, e.g.
  outp.write(outline.strip().encode('ascii', errors='replace') + '\n')

If you want to know the line which contains the error you will have to
modify your program to keep track of line numbers and trap and report


More information about the Tutor mailing list