python script Non-ASCII character

eryk sun eryksun at gmail.com
Sun Mar 19 22:50:43 EDT 2017


On Sun, Mar 19, 2017 at 11:06 PM, MRAB <python at mrabarnett.plus.com> wrote:
>
> If you're using Unicode string literals, your choices are:
>
> 1. Raw string literals:
>
>     var1 = ur"C:\Users\username\Desktop\η γλωσσα μου\mylanguage\myfile"

Raw unicode literals are practically useless in Python 2. They're not
actually raw because \u and \U are still parsed as Unicode escapes,
e.g. ur"C:\Users" and ur"C:\users" are syntax errors.

> 2. Slashes:
>
>     var1 = u"C:/Users/username/Desktop/η γλωσσα μου/mylanguage/myfile"

I prefer to normalize a path with os.path.normpath, or pathlib.Path in
Python 3. This converts slashes to backslashes to get a canonical
Windows path. IMO, it's also visually better in a text representation
or error messages. Otherwise at runtime joined paths often end up with
mixed slashes, which to me is ugly.



More information about the Python-list mailing list