[Python-porting] Unclear error message
Shai Berger
shai at platonix.com
Wed Apr 26 15:12:29 EDT 2017
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.
HTH,
Shai.
More information about the Python-porting
mailing list