Golshy Golshy - 2 years ago 112
Python Question

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

I am having trouble with re.search finding the string "Senders' Domains" on my UI.

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

When I Debug, my page_checker method finds "Senders//s Domains" but then re.search returns "Senders//sDomains" (removing the space).

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

Is there any way I can escape the single quote (') and space characters so that I can find and match the string "Senders' Domains" on my UI?

Answer Source

Have you tried the escape character '\'? https://docs.python.org/2.0/ref/strings.html

>>> word_check2 = "Senders\' Domains"
>>> print (word_check2)
Senders' Domains
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download