From 1a50d9623831990ae0a78ea3a7e66fa098fe92ac Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Thu, 16 Feb 2012 13:55:20 -0500 Subject: By default, turn unrecognized characters into numeric XML entity refs. --- bs4/doc/source/index.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'bs4/doc/source/index.rst') diff --git a/bs4/doc/source/index.rst b/bs4/doc/source/index.rst index 200317a..0467c00 100644 --- a/bs4/doc/source/index.rst +++ b/bs4/doc/source/index.rst @@ -2160,6 +2160,27 @@ element in the soup, just as if it were a Python string:: soup.p.encode("utf-8") # '

Sacr\xc3\xa9 bleu!

' +Any characters that can't be represented in your chosen encoding will +be converted into numeric XML entity references. For instance, here's +a document that includes the Unicode character SNOWMAN:: + + markup = u"\N{SNOWMAN}" + snowman_soup = BeautifulSoup(markup) + tag = snowman_soup.b + +The SNOWMAN character can be part of a UTF-8 document (it looks like +☃), but there's no representation for that character in ISO-Latin-1 or +ASCII, so it's converted into "☃" for those encodings:: + + print(tag.encode("utf-8")) + # + + print tag.encode("latin-1") + # + + print tag.encode("ascii") + # + Unicode, Dammit --------------- -- cgit v1.2.3