控制台中出现Python错误,但不在文件中:行连续字符后出现意外字符
我有一个Python脚本,它有一个用这个方法定义的类:控制台中出现Python错误,但不在文件中:行连续字符后出现意外字符,python,console,Python,Console,我有一个Python脚本,它有一个用这个方法定义的类: @staticmethod def _sanitized_test_name(orig_name): return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8'))) 如果我在粘贴时跳过该方法,它会起作用。请注意,我的原始行与错误显示的内容有所不同:r'[`\']*'vsr'[`''''']
@staticmethod
def _sanitized_test_name(orig_name):
return re.sub(r'[`‘’\"]*', '', re.sub(r'[\r\n\/\:\?\<\>\|\*\%]*', '', orig_name.encode('utf-8')))
如果我在粘贴时跳过该方法,它会起作用。请注意,我的原始行与错误显示的内容有所不同:r'[`\']*'
vsr'[`''''']*'
。将其替换为ur'[`'']*'
将在扫描字符串文字时给出SyntaxError:EOL
Python控制台似乎将'
视为一个样式化的`
(反勾选)而将'
视为一个系统化的'
(单引号)。当我真正指的是。我的脚本顶部有#-*-编码:utf-8-*-
,我也将其粘贴到控制台中。只关注导致错误的表达式r'[`'']*'
>>> r'[`‘’"]*'
File "<stdin>", line 1
r'[``'"]*'
^
SyntaxError: EOL while scanning string literal
>>> ur'[`‘’"]*' # with the unicode modifier
File "<stdin>", line 1
ur'[``'"]*'
^
SyntaxError: EOL while scanning string literal
(这个特定的正则表达式可能不需要原始字符串修饰符r'
)
>>> r'[`‘’"]*'
File "<stdin>", line 1
r'[``'"]*'
^
SyntaxError: EOL while scanning string literal
>>> ur'[`‘’"]*' # with the unicode modifier
File "<stdin>", line 1
ur'[``'"]*'
^
SyntaxError: EOL while scanning string literal
>>> r'[`' + unichr(2018) + unichr(2019) + r'"]*'
u'[`\u07e2\u07e3"]*'