Login

Convert Microsoft "smart quotes" to standard quotes

Author:
nomadjourney
Posted:
October 16, 2010
Language:
Python
Version:
1.2
Tags:
convert microsoft smart-quotes
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

  1. Filter to generate QR codes by javinievas 5 years ago
  2. entity encoded email-address by emna 6 years, 3 months ago
  3. Convert Unicode to ASCII by coordt 7 years, 5 months ago
  4. Handles Inline Formsets and also "in-standard-way" normal forms by sebnapi 3 years, 5 months ago
  5. "Autoconnect" model decorator, easy pre_save and post_save signal connection by bendavis78 5 years ago

Comments

Please login first before commenting.