我有一个在 kivy 下运行的程序,它在 Windows 上运行良好,但在 Android 上打开文件失败(两个平台上都是 1.8.0).奇怪的是,当我明确要求 utf-8 时,错误消息表明它正在尝试解码 ASCII;该文件的任何地方都没有 0xFF 字符 - 我使用 od
实用程序进行了检查.
I have a program running under kivy that works fine on Windows but fails on opening a file on Android (1.8.0 on both platforms). The odd thing is that the error message indicates it's trying to decode ASCII when I'm plainly asking for utf-8; also the file doesn't have a 0xFF character anywhere in it - I checked with the od
utility.
代码:
try:
Logger.info('Mark: opening file ' + repr(filename))
with codecs.open(filename, 'r', encoding='utf_8') as f:
lines = [line.rstrip(u'
').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'
') 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
我放入了一些调试代码以通过 整个列表,发现除了 ascii
、latin_1
和各种 utf
编解码器之外的所有内容都丢失了.
I put in some debugging code to go through the entire list and found that all were missing except ascii
, latin_1
, and the various utf
codecs.
摆脱try/except,看看真正的异常.codecs.BOM
是一个字节字符串 xffxfe
并且使用默认的 ascii
编解码器强制转换为 Unicode:
Get rid of the try/except and look at the real exception. codecs.BOM
is a byte string xffxfe
and it is being coerced into Unicode using the default ascii
codec:
>>> import codecs
>>> codecs.BOM
'xffxfe'
>>> 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)
这篇关于Android 上的 kivy/Python 上缺少 Unicode 编解码器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!