SmartyPants
SmartyPants is a Python library that converts plain ASCII punctuation in text to "smart" typographic HTML entities. It transforms straight quotes to "curly" quotes, backticks-style quotes, -- and --- to en- and em-dashes, and three consecutive dots to an ellipsis entity. The current version is 2.0.2, and it is actively maintained with recent fixes for Python 3.12+ compatibility.
Common errors
-
NameError: name 'smartypants' is not defined
cause The `smartypants` module was not imported before use.fixAdd `import smartypants` at the top of your Python file. -
TypeError: smartypants() takes X positional arguments but Y were given (where X and Y are numbers, often 1 and 2)
cause You are likely attempting to pass a string or incorrect type as an attribute argument to `smartypants.smartypants()`, which was removed in version 2.0.0.fixUse the `Attr` enum for specifying processing options. For example, `from smartypants import Attr; smartypants.smartypants(text, attrs=Attr.q | Attr.d)`. -
UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>
cause This error can occur when processing text with unexpected encodings, particularly when running on systems with default encodings that don't match the input text (e.g., Python 3 with specific locales handling Unicode characters).fixEnsure your input text is consistently encoded, typically UTF-8. Explicitly decode input if reading from a file, for example, `text = open('input.txt', encoding='utf-8').read()`.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes, including dropping Pyblosxom support, removing string-type attributes in favor of `Attr` enums, and deprecating old function names like `smartyPants`, `educateQuotes`, and `processEscapes`.
- gotcha SmartyPants processes most plain text, but it deliberately skips certain HTML elements (e.g., `<pre>`, `<code>`, `<span>`, `<script>`, `<style>`) by default to prevent unintended conversion of code or other literal content.
- gotcha To prevent 'smart' conversions for literal straight quotes, hyphens, or periods (e.g., '6\'2"' instead of '6\u20192\u201d'), use backslash escapes (`\`, `\-`, `\.`).
Install
-
pip install smartypants
Imports
- smartypants
import smartypants
- Attr
from smartypants import Attr
Quickstart
import smartypants text = '"SmartyPants" is smart, so is <code>smartypants</code> -- a Python port...' processed_text = smartypants.smartypants(text) print(processed_text) # Example with attributes from smartypants import Attr attrs = Attr.q | Attr.d # Enable quotes and dashes processed_text_with_attrs = smartypants.smartypants(text, attrs) print(processed_text_with_attrs)