473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

smart quotes

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.
Aug 26 '08 #1
2 7997
Adrian Smith wrote:
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:
>>s = "a “smart quote” example"
u = s.decode("utf-8")
Now you can replace the quotes (I looked up the codes in wikipedia):
>>u.replace(u"\u201c", "").replace(u"\u201d", "")
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
Aug 26 '08 #2
On Aug 26, 4:13*pm, Peter Otten <__pete...@web.dewrote:
Adrian Smith wrote:
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:
>s = "a “smart quote” example"
u = s.decode("utf-8")

Now you can replace the quotes (I looked up the codes in wikipedia):
>u.replace(u"\u201c", "").replace(u"\u201d", "")

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!
Aug 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Martin Goldman | last post by:
Hello all, I've been struggling for a few days with the question of how to convert "smart" (curly) quotes into straight quotes. I tried playing with the htmlentities() function, but all that is...
1
by: Max | last post by:
Anyone have a smart-quotes algorithm? Preferably in Java; python 2nd best; otherwise anything I can grok. Is there anything better (smarter) than: 1) if the previous character was...
11
by: Ron | last post by:
Hello, I'm having an aggravating time getting the "html" spewed by Word 2003 to display correctly in a webpage. The situation here is that the people creating the documents only know Word, and...
37
by: Ian Rastall | last post by:
I've been working on an online books site for almost four years now, and have been putting smart quotes in each book. This is a major hassle, and I'm beginning to think it's not worth it. Is...
3
by: red floyd | last post by:
I've got some code where somebody cut&pasted some comments from MS Word, and so these comments have "smart quotes" (in particular apostrophes) embedded. The apostrophe is character hex 0x92. ...
3
by: Sean S - Perth, WA | last post by:
Hi all, I'm wondering if there is a way to find (to strip or process) smart quotes in text submitted via a form? These don't work: strOutput = Replace(strOutput, "“", "“") ' left...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.