smart quotes

A

Adrian Smith

Can anyone tell me how to get rid of smart quotes in html using
Python? I've tried variations on
stuff = string.replace(stuff, "\“", "\""), but to no avail, presumably
because they're not standard ASCII.
 
P

Peter Otten

Adrian said:
Can anyone tell me how to get rid of smart quotes in html using
Python? I've tried variations on
stuff = string.replace(stuff, "\“", "\""), but to no avail, presumably
because they're not standard ASCII.

Convert the string to unicode. For that you have to know its encoding. I
assume UTF-8:

Now you can replace the quotes (I looked up the codes in wikipedia):
u'a smart quote example'

Alternatively, if you have many characters to remove translate() is more
efficient:
u.translate(dict.fromkeys([0x201c, 0x201d, 0x2018, 0x2019]))
u'a smart quote example'

If necessary convert the result back to the original encoding:
clean = u.translate(dict.fromkeys([0x201c, 0x201d, 0x2018, 0x2019]))
clean.encode("utf-8")
'a smart quote example'

Peter
 
A

Adrian Smith

Adrian said:
Can anyone tell me how to get rid of smart quotes in html using
Python? I've tried variations on
stuff = string.replace(stuff, "\“", "\""), but to no avail, presumably
because they're not standard ASCII.

Convert the string to unicode. For that you have to know its encoding. I
assume UTF-8:

Now you can replace the quotes (I looked up the codes in wikipedia):

u'a smart quote example'

Alternatively, if you have many characters to remove translate() is more
efficient:
u.translate(dict.fromkeys([0x201c, 0x201d, 0x2018, 0x2019]))

u'a smart quote example'

If necessary convert the result back to the original encoding:
clean = u.translate(dict.fromkeys([0x201c, 0x201d, 0x2018, 0x2019]))
clean.encode("utf-8")

'a smart quote example'

Peter

Brilliant, thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top