encutils - encoding detection collection for Python
Version: | 0.9.8 |
---|---|
Author: | Christof Hoeke, see http://cthedot.de/encutils/ |
Contributor: | Robert Siemer, Fredrik Hedman <fredrik.hedman@me.com> ported to python3 |
Copyright: | 2005-2012: Christof Hoeke |
License: | encutils has a dual-license, please choose whatever you prefer:
encutils is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. encutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with encutils. If not, see <http://www.gnu.org/licenses/>. |
A collection of helper functions to detect encodings of text files (like HTML, XHTML, XML, CSS, etc.) retrieved via HTTP, file or string.
getEncodingInfo() is probably the main function of interest which uses other supplied functions itself and gathers all information together and supplies an EncodingInfo object.
example:
>>> import encutils
>>> info = encutils.getEncodingInfo(url='http://cthedot.de/encutils/')
>>> str(info)
utf-8
>>> repr(info)
<encutils.EncodingInfo object encoding='utf-8' mismatch=False at...>
>>> info.logtext
HTTP media_type: text/html
HTTP encoding: utf-8
Encoding (probably): utf-8 (Mismatch: False)
Helper to build a basic log
Returns: | a log with the name logname |
---|
Parameters: | media_type – a media type like “text/html” |
---|---|
Returns: | a default encoding for given media_type. For example
"utf-8" for media_type="application/xml". If no default encoding is available returns None. Refers to RFC 3023 and HTTP MIME specification. |
Parameters: | response – a HTTP response object |
---|---|
Returns: | (media_type, encoding) information from the response Content-Type HTTP header. (Case of headers is ignored.) May be (None, None) e.g. if no Content-Type header is available. |
Parameters: | text – a byte string |
---|---|
Returns: | (media_type, encoding) information from (first)
X/HTML Content-Type <meta> element if available in text. XHTML format: <meta http-equiv="Content-Type"
content="media_type;charset=encoding" />
|
Attempt to detect the character encoding of the xml file given by a file object fp. fp must not be a codec wrapped file object! fp may be a string or unicode string though.
Based on a recipe by Lars Tiede: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/363841 which itself is based on Paul Prescotts recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52257
Returns: |
|
---|
Find all encoding related information in given text.
Information in headers of supplied HTTPResponse, possible XML declaration and X/HTML <meta> elements are used.
Parameters: |
|
---|---|
Returns: | instance of EncodingInfo. |
How the resulting encoding is retrieved:
RFC 3023 states if media type given in the Content-Type HTTP header is application/xml, application/xml-dtd, application/xml-external-parsed-entity, or any one of the subtypes of application/xml such as application/atom+xml or application/rss+xml etc then the character encoding is determined in this order:
1. the encoding given in the charset parameter of the Content-Type HTTP header, or 2. the encoding given in the encoding attribute of the XML declaration within the document, or 3. utf-8.
If the media type given in the Content-Type HTTP header is text/xml, text/xml-external-parsed-entity, or a subtype like text/Anything+xml, the encoding attribute of the XML declaration is ignored completely and the character encoding is determined in the order: 1. the encoding given in the charset parameter of the Content-Type HTTP header, or 2. ascii.
No mismatch possible.
If no media type is given the XML encoding pseuso attribute is used if present.
No mismatch possible.
If installed uses chardet http://chardet.feedparser.org/ to detect encoding, else tries different encodings on text and returns the one that does not raise an exception which is not very advanced or may be totally wrong. The tried encoding are in order ‘ascii’, ‘iso-8859-1’, ‘windows-1252’ (which probably will never happen as ‘iso-8859-1’ can decode these strings too) and lastly ‘utf-8’.
Parameters: | text – a byte string |
---|---|
Returns: | Working encoding or None if no encoding does work at all. The returned encoding might nevertheless be not the one intended by the author as it is only checked if the text might be encoded in that encoding. Some texts might be working in “iso-8859-1” and “windows-1252” and “ascii” and “utf-8” and ... |
All encoding related information, returned by getEncodingInfo().
Encoding is the explicit or implicit encoding or None and always lowercase.
header. Mismatch is True if any mismatches between HTTP header, XML declaration or textcontent (meta) are found. More detailed mismatch reports are written to the optional log or logtext
Mismatches are not necessarily errors as preferences are defined. For details see the specifications.
logtext: if no log was given log reports are given here