如何在 Python 中转义单引号和空格?



我在 re.search 上查找字符串"发件人的域"时遇到问题。

word_check1 = 'Senders'
word_check2 = "Senders' Domains"
page_check1, page_check2 = mymessages_page.page_checker(word_check1, word_check2, 'chart-content-container')

当我调试时,我的page_checker方法找到"发件人//s域",但随后re.search返回"发件人//s域"(删除空格(。

def page_checker(self, word_check1, word_check2, ids):
container = self.driver.find_element_by_id(ids)
content = container.text
organized_container_content = content.split('n')
for text in organized_container_content:
if re.search(word_check1, text):
check1 = text
for text2 in organized_container_content:
if re.search(word_check2, text2):
check2 = text2
return check1, check2
break

有什么方法可以转义单引号 ('( 和空格字符,以便我可以在我的 UI 上找到并匹配字符串"发件人的域"?

您是否尝试过转义字符"\"? https://docs.python.org/2.0/ref/strings.html

>>> word_check2 = "Senders' Domains"
>>> print (word_check2)
Senders' Domains
>>> 

最新更新