缺少统一code codeCS上kivy / Python的在Android? [英] Missing Unicode codecs on kivy/Python on Android?
问题描述
我下kivy在Windows上工作正常,但未能上打开在Android(1.8.0两个平台上)的文件运行的程序。奇怪的是,该错误信息表明它试图去时,我清楚地要求UTF-8 code 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.
在code:
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
编辑:我把一些调试code要经过的整个列表,发现所有被除 ASCII
, LATIN_1 $ C失踪$ C>,以及各种
UTF
codeCS。
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.
推荐答案
获取/去掉尝试,除了看看真正的例外。 codecs.BOM
是一个字节的字符串 \\ XFF \\ XFE
键,它被强迫统一code。使用默认的 ASCII
codeC:
Get rid of the try/except and look at the real exception. codecs.BOM
is a byte string \xff\xfe
and it is being coerced into Unicode using the default ascii
codec:
>>> 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)
这篇关于缺少统一code codeCS上kivy / Python的在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!