2

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/'

3 Answers 3

2

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"'
>>>
Sign up to request clarification or add additional context in comments.

Comments

2

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/'
'''

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.