gpt4 book ai didi

python重新匹配unicode字符

转载 作者:行者123 更新时间:2023-11-28 22:05:23 26 4
gpt4 key购买 nike

无法将 unicode 字符与 python 中的正则表达式匹配

# -*- 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 字符!

最佳答案

因为 locations 不是 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重新匹配unicode字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5492921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com