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. 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
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |