- Author:
- nomadjourney
- Posted:
- October 16, 2010
- Language:
- Python
- Version:
- 1.2
- Score:
- 2 (after 2 ratings)
This snippet of code will convert those pesky "smart quote" characters (usually from Microsoft products) to standard quotes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from django.utils.encoding import smart_unicode
# translation mapping table that converts
# single smart quote characters to standard
# single quotes
SINGLE_QUOTE_MAP = {
0x2018: 39,
0x2019: 39,
0x201A: 39,
0x201B: 39,
0x2039: 39,
0x203A: 39,
}
# translation mapping table that converts
# double smart quote characters to standard
# double quotes
DOUBLE_QUOTE_MAP = {
0x00AB: 34,
0x00BB: 34,
0x201C: 34,
0x201D: 34,
0x201E: 34,
0x201F: 34,
}
def convert_smart_quotes(str):
"""
Convert "smart quotes" from Microsoft products
to standard quotes.
"""
return smart_unicode(str).translate(DOUBLE_QUOTE_MAP).translate(SINGLE_QUOTE_MAP)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.