簡體   English   中英

如何在Python中轉義單引號和空格?

[英]How can I escape a single quote and a space in Python?

我在re.search上找到UI上的字符串“ 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域”,但是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”?

您是否嘗試過轉義字符“ \\”? https://docs.python.org/2.0/ref/strings.html

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM