From kq3f at comcast.net  Wed Apr 26 11:23:00 2017
From: kq3f at comcast.net (Joe Stepansky)
Date: Wed, 26 Apr 2017 11:23:00 -0400
Subject: [Python-porting] Unclear error message
Message-ID: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>

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

 

Huh? I tried removing the parentheses but, of course, that was also a syntax
error. Checked the online 3.6 docs for "with open" and my code looks OK. But
it isn't. Any suggestions welcome.

 

Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20170426/89140b6e/attachment.html>

From dmalcolm at redhat.com  Wed Apr 26 12:43:14 2017
From: dmalcolm at redhat.com (David Malcolm)
Date: Wed, 26 Apr 2017 12:43:14 -0400
Subject: [Python-porting] Unclear error message
In-Reply-To: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
References: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
Message-ID: <1493224994.9106.155.camel@redhat.com>

On Wed, 2017-04-26 at 11:23 -0400, Joe Stepansky wrote:
> 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
>  
> Huh? I tried removing the parentheses but, of course, that was also a
> syntax error. Checked the online 3.6 docs for ?with open? and my code
> looks OK. But it isn?t. Any suggestions welcome.

The backslash in the path is being interpreted by Python as the first
part of an escape sequence, and "\U" has special meaning.

Have a look at:
http://stackoverflow.com/questions/12953683/backslashes-in-windows-file
path

for some ideas for workarounds.


Dave



From mmueller at python-academy.de  Wed Apr 26 12:59:24 2017
From: mmueller at python-academy.de (=?UTF-8?Q?Mike_M=c3=bcller?=)
Date: Wed, 26 Apr 2017 18:59:24 +0200
Subject: [Python-porting] Unclear error message
In-Reply-To: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
References: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
Message-ID: <5a79cca7-d8e2-13b8-e048-d0ad9c9ac781@python-academy.de>

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'

Mike

> 
>  
> 
> Huh? I tried removing the parentheses but, of course, that was also a syntax
> error. Checked the online 3.6 docs for ?with open? and my code looks OK. But it
> isn?t. Any suggestions welcome.
> 
>  
> 
> Joe
> 
> 
> 
> _______________________________________________
> Python-porting mailing list
> Python-porting at python.org
> https://mail.python.org/mailman/listinfo/python-porting
> 


From shai at platonix.com  Wed Apr 26 15:12:29 2017
From: shai at platonix.com (Shai Berger)
Date: Wed, 26 Apr 2017 22:12:29 +0300
Subject: [Python-porting] Unclear error message
In-Reply-To: <5a79cca7-d8e2-13b8-e048-d0ad9c9ac781@python-academy.de>
References: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
 <5a79cca7-d8e2-13b8-e048-d0ad9c9ac781@python-academy.de>
Message-ID: <201704262212.30222.shai@platonix.com>

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.

From brett at python.org  Thu Apr 27 15:50:41 2017
From: brett at python.org (Brett Cannon)
Date: Thu, 27 Apr 2017 19:50:41 +0000
Subject: [Python-porting] Unclear error message
In-Reply-To: <201704262212.30222.shai@platonix.com>
References: <014001d2bea0$fdc0ee10$f942ca30$@comcast.net>
 <5a79cca7-d8e2-13b8-e048-d0ad9c9ac781@python-academy.de>
 <201704262212.30222.shai@platonix.com>
Message-ID: <CAP1=2W6X78NanuALQ=b6sP+Rx+0L_o40bYEPvxC9vqUdgCePvA@mail.gmail.com>

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>