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

时间:2017-08-16 14:50:48

标签: python regex selenium-webdriver escaping

我在re.search上找到字符串“Senders'Domains”时遇到了麻烦。

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

当我调试时,我的page_checker方法找到“Senders // s Domains”但是re.search返回“Senders // sDomains”(删除空格)。

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上找到并匹配字符串“Senders'Domains”?

1 个答案:

答案 0 :(得分:1)

你试过转义字符'\'吗? https://docs.python.org/2.0/ref/strings.html

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