简体   繁体   中英

Raw unicode literal that is valid in Python 2 and Python 3?

Apparently the ur"" syntax has been disabled in Python 3. However, I need it! "Why?" , you may ask. Well, I need the u prefix because it is a unicode string and my code needs to work on Python 2. As for the r prefix, maybe it's not essential, but the markup format I'm using requires a lot of backslashes and it would help avoid mistakes.

Here is an example that does what I want in Python 2 but is illegal in Python 3:

tamil_letter_ma = u"\u0bae"
marked_text = ur"\a%s\bthe Tamil\cletter\dMa\e" % tamil_letter_ma

After coming across this problem, I found http://bugs.python.org/issue15096 and noticed this quote:

It's easy to overcome the limitation.

Would anyone care to offer an idea about how?

Related: What exactly do "u" and "r" string flags do in Python, and what are raw string literals?

Why don't you just use raw string literal ( r'....' ), you don't need to specify u because in Python 3, strings are unicode strings.

>>> tamil_letter_ma = "\u0bae"
>>> marked_text = r"\a%s\bthe Tamil\cletter\dMa\e" % tamil_letter_ma
>>> marked_text
'\\aம\\bthe Tamil\\cletter\\dMa\\e'

To make it also work in Python 2.x, add the following Future import statement at the very beginning of your source code, so that all the string literals in the source code become unicode.

from __future__ import unicode_literals

The preferred way is to drop u'' prefix and use from __future__ import unicode_literals as @falsetru suggested . But in your specific case, you could abuse the fact that "ascii-only string" % unicode returns Unicode:

>>> tamil_letter_ma = u"\u0bae"
>>> marked_text = r"\a%s\bthe Tamil\cletter\dMa\e" % tamil_letter_ma
>>> marked_text
u'\\a\u0bae\\bthe Tamil\\cletter\\dMa\\e'

Unicode字符串是Python 3.x中的默认字符串,因此单独使用r将生成与Python 2中的ur相同的字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM