I am trying to few junk characters in my string using the following statement:
desc = string.replace(desc,'“','"')
desc = string.replace(desc,'”','"')
desc = string.replace(desc,'·','.')
Can I write the above 3 statements in to a single statement or atlease the 1st two statements to a single statement.
I can not use any third-party libraries in my project.
Edit @unutbu:
My String looks like below:
This is
'“' my teststring '”'.I want to replace unicode with appropriate HTML not the whole string only with unicode values.
After using the code:
import HTMLParser
text='“ ” ·'
parser=HTMLParser.HTMLParser()
desc=parser.unescape(text)
I am getting only the HTML equivalents , not the string. But I just want to replace the appropriate values keeping everything in Original String.
I expect the out put as below:
This is “my teststring”. I want to replace unicode with appropriate HTML not the whole string only with unicode values.
HTMLParser is in the standard library:
If you want that in a single statement, you could of course do
but that might not be an advantage if you need to call
unescapein more than one place, and in general, chaining calls like this makes it harder to identify where exceptions occur.Note that
HTMLParser.unescapewill unescape all HTML entities defined inhtmlentitydefs.names2codepoint(plus').Edit: HTMLParser.unescape returns different characters than what you posted.
To get exactly those characters, you might use xml.sax.saxutils:
Note that
saxutils.unescapealso replaces<,>and&. If you wish to replace only“,”, and·, then I’d use aix’s answer.