[Python-porting] Unclear error message
Brett Cannon
brett at python.org
Thu Apr 27 15:50:41 EDT 2017
On Wed, 26 Apr 2017 at 12:22 Shai Berger <shai at platonix.com> wrote:
> On Wednesday 26 April 2017 19:59:24 Mike Müller wrote:
> > Am 26.04.17 um 17:23 schrieb Joe Stepansky:
> > > I’ve decided to just jump into 3.6 by porting one of my Python 2.7
> > > programs. The first error I received was an easy one to resolve, but
> now
> > > I have one I don’t understand. Here’s the code:
> > >
> > > with open(‘C:\Users\Joe\METAR.filelist.txt’,’r’) as METAR_filelist:
> > > ^
> > >
> > > This gives me an error message with a “caret” below the first (open)
> > > parentheses and the following message:
> > >
> > >
> > >
> > > SyntaxError: (Unicode error) ‘unicodeescape’ codec can’t decode bytes
> in
> > > position 2-3: truncated \UXXXXXXXX escape
> >
> > This has nothing to do with open. The `\U....` specifies a certain
> Unicode
> > character, that does not exits. ;)
> >
> > Solution:
> >
> > On Windows, when copying and pasting paths always use raw strings
> starting
> > with an "r". So, this will solve your problem:
> >
> > r'C:\Users\Joe\METAR.filelist.txt'
> >
>
> Actually, you should be able to reproduce this error on Python 2.7 by
> adding a
> future import, as the first line in the file:
>
> from __future__ import unicode_literals
>
> Take a look at all the things you can import from __future__. Adding these,
> while still running Python 2.7, should probably be the first step for
> porting a
> file from Python 2 to Python 3.
>
Or stick a 'u' prefix on the string. People seem to be hurt more than
helped in porting code with unicode_literals, so we typically advise people
to label *every *string literal with either a 'u' or 'b' prefix.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20170427/6f6e8ca1/attachment.html>
More information about the Python-porting
mailing list