4

以下代码:

s = s.replace(u"&", u"&")

在python中导致错误:

SyntaxError: invalid syntax

在解决问题之前删除u's ",但这应该按原样工作吗?我正在使用 Python 3.1

4

3 回答 3

11

The u is no longer used in Python 3. String literals are unicode by default. See What's New in Python 3.0.

You can no longer use u"..." literals for Unicode text. However, you must use b"..." literals for binary data.

于 2011-09-27T12:23:47.667 回答
3

On Python 3, strings are unicode. There is no need to (and as you've discovered, you can't) put a u before the string literal to designate unicode.

Instead, you have to put a b before a byte literal to designate that it isn't unicode.

于 2011-09-27T12:24:13.463 回答
1

在 Python3.3+ 中,unicode 文字再次有效,请参阅Python 3.3 中的新增功能

新的语法特性:

生成器委托表达式的新产量。
str 对象再次接受 u'unicode' 语法。

于 2015-03-19T15:55:15.990 回答