Skip to content

Instantly share code, notes, and snippets.

@davidtheclark
Created May 5, 2013 17:10
Show Gist options
  • Save davidtheclark/5521432 to your computer and use it in GitHub Desktop.
Save davidtheclark/5521432 to your computer and use it in GitHub Desktop.
Convert dumb quotes to smart quotes in Python
def dumb_to_smart_quotes(string):
"""Takes a string and returns it with dumb quotes, single and double,
replaced by smart quotes. Accounts for the possibility of HTML tags
within the string."""
# Find dumb double quotes coming directly after letters or punctuation,
# and replace them with right double quotes.
string = re.sub(r'([a-zA-Z0-9.,?!;:\'\"])"', r'\1”', string)
# Find any remaining dumb double quotes and replace them with
# left double quotes.
string = string.replace('"', '“')
# Reverse: Find any SMART quotes that have been (mistakenly) placed around HTML
# attributes (following =) and replace them with dumb quotes.
string = re.sub(r'=“(.*?)”', r'="\1"', string)
# Follow the same process with dumb/smart single quotes
string = re.sub(r"([a-zA-Z0-9.,?!;:\"\'])'", r'\1’', string)
string = string.replace("'", '‘')
string = re.sub(r'=‘(.*?)’', r"='\1'", string)
return string
@dmdeluca
Copy link

I wrote a method to do the opposite, ignoring anything in an HTML tag: https://gist.github.com/dmdeluca/9cffec2edad3d9282dea534692f5b702

Might not be perfect, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment