File: unicode.rst

package info (click to toggle)
python-xlrd 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,832 kB
  • sloc: python: 7,531; makefile: 118; sh: 7
file content (37 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (4)
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
33
34
35
36
37
Handling of Unicode
===================

This package presents all text strings as Python unicode objects.
From Excel 97 onwards, text in Excel spreadsheets has been stored as `UTF-16LE
<http://unicode.org/faq/utf_bom.html />`_
(a 16-bit Unicode Transformation Format).
Older files (Excel 95 and earlier) don't keep strings in Unicode;
a ``CODEPAGE`` record provides a codepage number (for example, 1252) which is
used by xlrd to derive the encoding (for same example: "cp1252") which is
used to translate to Unicode.

If the ``CODEPAGE`` record is missing (possible if the file was created
by third-party software), ``xlrd`` will assume that the encoding is ascii,
and keep going. If the actual encoding is not ascii, a
:class:`UnicodeDecodeError` exception will be raised and
you will need to determine the encoding yourself, and tell xlrd:

.. code-block:: python

  book = xlrd.open_workbook(..., encoding_override="cp1252")

If the ``CODEPAGE`` record exists but is wrong (for example, the codepage
number is 1251, but the strings are actually encoded in koi8_r),
it can be overridden using the same mechanism.

The supplied ``runxlrd.py`` has a corresponding command-line argument, which
may be used for experimentation:

.. code-block:: bash

    runxlrd.py -e koi8_r 3rows myfile.xls

The first place to look for an encoding, the "codec name", is
`the Python documentation`__.

__ https://docs.python.org/library/codecs.html#standard-encodings