[英]how can I compare “hello:”==“hello” in python
我想忽略字符串中的符号,并与非符号字符串name="Avengers Endgame"
find_element_by_link_text(name.title()).click()
<a href="#">Avengers: Endgame</a>
You could use a regex on the word class ( \\W
). 您可以在单词class(
\\W
)上使用正则表达式 。 From the link, 通过链接,
\\W Matches any character which is not a word character. This is the opposite of \\w. If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_]. If the LOCALE flag is used, matches characters considered alphanumeric in the current locale and the underscore.
Like, 喜欢,
import re
a = 'Avengers Endgame'
b = 'Avengers: Endgame'
if re.sub(r'[\W]', '', a) == re.sub(r'[\W]', '', b):
print("They match")
I wrote this function for another project. 我为另一个项目编写了此函数。 Use re module, I guess it can help you.
使用re模块,我想它可以为您提供帮助。
def convert_string(str):
'''
return the string without any signs
ex :
input = 'abcdefghijklmnopqrstuvwxyz123456789!@#$%^&*()-=_+'
ouput = 'abcdefghijklmnopqrstuvwxyz123456789'
'''
pat = re.compile(r'[^a-zA-Z0-9]', flags=re.IGNORECASE)
return pat.sub('', str)
you could use a generator expression to only yield characters from your string that are alphanumeric or whitespace, then join them back into a new string: 您可以使用生成器表达式从字符串中仅产生字母数字或空格字符,然后将它们重新组合成新的字符串:
text = 'Avengers: Endgame'
stripped_text = ''.join(char for char in text if char.isalnum() or char.isspace())
your new string would then be: Avengers Endgame
您的新字符串将是:
Avengers Endgame
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.