python重新匹配unicode字符
在python中将unicode字符与正则表达式匹配时遇到问题python重新匹配unicode字符,python,regex,unicode,Python,Regex,Unicode,在python中将unicode字符与正则表达式匹配时遇到问题 # -*- coding: utf8 -*- import re locations = [ "15°47'S 47°55'W", "21º 18' N, 157º 51' W", "32°46′58″N 96°48′14″W", ] rx = re.compile(ur""" ^\d+[°º] | ^\d+[\xb0\xba] """, re.X) for loc i
# -*- coding: utf8 -*-
import re
locations = [
"15°47'S 47°55'W",
"21º 18' N, 157º 51' W",
"32°46′58″N 96°48′14″W",
]
rx = re.compile(ur"""
^\d+[°º]
|
^\d+[\xb0\xba]
""", re.X)
for loc in locations:
if not rx.match(loc):
print loc
结果:
15°47'S 47°55'W
21º 18' N, 157º 51' W
32°46′58″N 96°48′14″W
似乎无法匹配unicode字符 因为
位置
不是unicode字符串
locations = [
u"15°47'S 47°55'W",
u"21º 18' N, 157º 51' W",
u"32°46′58″N 96°48′14″W",
]
这没关系,Python会自动转换。@phihag:
print re.match(u“,”)
在Python2.7中没有。哦,你完全正确。只有当双方都是ASCII的子集时,这种“自动转换”才起作用。