Android上的kivy / Python上缺少Unicode编解码器?

时间:2014-07-17 03:47:22

标签: android python unicode kivy

我有一个在kivy下运行的程序在Windows上工作正常但在Android上打开文件失败(在两个平台上都是1.8.0)。奇怪的是,当我明确要求utf-8时,错误消息表明它正在尝试解码ASCII;该文件中的任何位置都没有0xFF字符 - 我使用od实用程序进行了检查。

代码:

try:
    Logger.info('Mark: opening file ' + repr(filename))
    with codecs.open(filename, 'r', encoding='utf_8') as f:
        lines = [line.rstrip(u'\r\n').lstrip(codecs.BOM) for line in f]
except UnicodeDecodeError as e:
    Logger.info('Mark: utf8 load failed: ' + str(e))
    with codecs.open(filename, 'r', encoding='cp1252') as f:
        lines = [line.rstrip(u'\r\n') for line in f]

日志文件:

[INFO              ] Mark: opening file '/mnt/sdcard/My Files/Documents/DVDs.txt'
[INFO              ] Mark: utf8 load failed: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
[INFO              ] Base: Leaving application in progress...
[WARNING           ] stderr: Traceback (most recent call last):
[WARNING           ] stderr:   File "main.py", line 295, in <module>
[WARNING           ] stderr:     app.run()
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/app.py", line 792, in run
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/base.py", line 481, in runTouchApp
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/core/window/window_pygame.py", line 381, in mainloop
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/core/window/window_pygame.py", line 287, in _mainloop
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/base.py", line 321, in idle
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 422, in tick
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 537, in _process_events
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/site-packages/kivy/clock.py", line 309, in tick
[WARNING           ] stderr:   File "main.py", line 266, in select_file_part2
[WARNING           ] stderr:     with codecs.open(filename, 'r', encoding='cp1252') as f:
[WARNING           ] stderr:   File "/home/tito/code/python-for-android/build/python-install/lib/python2.7/codecs.py", line 884, in open
[WARNING           ] stderr: LookupError: unknown encoding: cp1252

编辑:我输入了一些调试代码来浏览entire list,发现除了asciilatin_1和各种{utf之外都丢失了所有内容{1}}编解码器。

1 个答案:

答案 0 :(得分:3)

摆脱try / except并查看真正的异常。 codecs.BOM是一个字节字符串\xff\xfe,它使用默认的ascii编解码器强制转换为Unicode:

>>> import codecs
>>> codecs.BOM
'\xff\xfe'
>>> u'test'.lstrip(codecs.BOM)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)