gpt4 book ai didi

python - unicode 正则表达式匹配 - 如何搜索复选标记? ✓

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:46 26 4
gpt4 key购买 nike

我正在尝试匹配其中包含复选标记的行:✓

我正在使用 python3。

unicode 编码可以在这里阅读:https://codepoints.net/U+2713?lang=en

我要匹配的行看起来像这样:

✓ Chrome on MAC - MySite.com - version-1

re.match("✓", line) 不起作用。 re.match("/u2713", line) 也不起作用。

如何确定是否包含✓?

---更新---

已解决:显然在 ✓ 之前有某种不可见的字符,这导致 match 运算符失败。感谢@NickT 和@EricDuminil 为我提供线索。此外,in 运算符似乎更简单、更安全,因此我将该答案标记为正确。

最佳答案

您甚至不需要任何正则表达式。你可以使用 in operator :

>>> "✓" in "✓ Chrome on MAC - MySite.com - version-1"
True
>>> "✓" in "Chrome on MAC - MySite.com - version-1"
False

如果你想在'marks.txt'中显示带复选标记的行,你可以这样写:

with open('marks.txt') as f:
for line in f:
if "✓" in line:
print(line, end='')

关于python - unicode 正则表达式匹配 - 如何搜索复选标记? ✓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46103373/

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