Question

I have this string that is basically some html

<acronym class="non-experimental-qualifier" title="Indicates that the information given is not based on experimental findings." onclick="dialog('non_experimental_qualifiers'); Event.stop(event || window.event); return false" onmousedown="Event.stop(event || window.event);">By similarity</acronym>. <a class="attribution" href="http://hamap.expasy.org/unirule/

is there anyway i can read it as a string in python . . . usually it only has one type of ' or " so i can just use the other to string it but . . . this has both

how can i turn this into a string ? like:

html = '<acronym class="non-experimental-qualifier" title="Indicates that the information given is not based on experimental findings." onclick="dialog('non_experimental_qualifiers'); Event.stop(event || window.event); return false" onmousedown="Event.stop(event || window.event);">By similarity</acronym>. <a class="attribution" href="http://hamap.expasy.org/unirule/'
Was it helpful?

Solution

You can use a triple-quoted string:

html = '''<acronym class="non-experimental-qualifier" title="Indicates that the information given is not based on experimental findings." onclick="dialog('non_experimental_qualifiers'); Event.stop(event || window.event); return false" onmousedown="Event.stop(event || window.event);">By similarity</acronym>. <a class="attribution" href="http://hamap.expasy.org/unirule/'''

Below is a demonstration:

>>> mystr = '''It's a "string"'''
>>> mystr
'It\'s a "string"'
>>>

OTHER TIPS

You can escape quotes with \' and \", or you can use triple quotes:

example1 = 'She\'s fond of saying "Hello"'
example2 = "She's fond of saying \"Hello\""
example3 = '''She's fond of saying "Hello"'''
example4 = """She's fond of saying "Hello\""""

Be careful with the closing " at the end in the triple-quoted string; it has to be escaped still to not count for the last 3 closing quotes.

Demo:

>>> example1 = 'She\'s fond of saying "Hello"'
>>> example2 = "She's fond of saying \"Hello\""
>>> example3 = '''She's fond of saying "Hello"'''
>>> example4 = """She's fond of saying "Hello\""""
>>> example1 == example2 == example3 == example4
True

In addition, newlines are permitted and preserved in a triple-quoted string, making for a very readable literal HTML value:

html = '''\
<acronym class="non-experimental-qualifier" 
         title="Indicates that the information given is not based on experimental findings." 
         onclick="dialog('non_experimental_qualifiers'); Event.stop(event || window.event); return false" 
         onmousedown="Event.stop(event || window.event);">By similarity</acronym>. 
    <a class="attribution" href="http://hamap.expasy.org/unirule/'
'''

I am confused - to read the string from an external object you don't need to do anything. Python has batteries included and will handle the quotation marks. I took your string and saved it in a file and then read it using open().read()

>>>mystring = open('c:\\mytest.txt').read()

>>>mystring
'<acronym class="non-experimental-qualifier" title="Indicates that the information given is not based on experimental findings." onclick="dialog(\'non_experimental_qualifiers\'); Event.stop(event || window.event); return false" onmousedown="Event.stop(event || window.event);">By similarity</acronym>. <a class="attribution" href="http://hamap.expasy.org/unirule/'

So my question is, do you mean read or are you trying to construct a string like the one above?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top