【发布时间】:2014-01-13 09:07:14
【问题描述】:
我只想让 python 代码工作,但我不理解这些转换错误(我总是得到某种类型的“ascii”编码或解码错误)。我发疯了,对生产线的每一部分都进行了解码和编码,但它仍然给我带来了麻烦。如果您愿意纠正它,可以通过 GIT 在https://github.com/TBOpen/papercut 获得它(我还使用self.wfile.write(message.decode('cp1250', 'replace').encode('ascii', 'replace') + "\r\n") 解决了未在第 885 行签入的类似错误。
但是,这是我无法解决的问题(我放弃的地方)的回溯。
Traceback (most recent call last):
File "/usr/local/lib/python2.6/SocketServer.py", line 535, in process_request
self.finish_request(request, client_address)
File "/usr/local/lib/python2.6/SocketServer.py", line 320, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.6/SocketServer.py", line 615, in __init__
self.handle()
File "./papercut.py", line 221, in handle
getattr(self, "do_%s" % (command))()
File "./papercut.py", line 410, in do_ARTICLE
self.send_response("%s\r\n%s\r\n\r\n%s\r\n.".decode('cp1250', 'replace').encode('ascii', 'replace') % (response.decode('cp1250', 'replace').encode('ascii', 'replace'), result[0].decode('cp1250', 'replace').encode('ascii', 'replace'), result[1].decode('cp1250', 'replace').encode('ascii', 'replace')))
File "/usr/local/lib/python2.6/encodings/cp1250.py", line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2122' in position 20: ordinal not in range(128)
TIA!!
【问题讨论】:
-
你使用的是python 2.x还是python 3.x?
-
@mig-25foxbat:Python 2.6,来自追溯。
-
你能把你的py代码贴出来吗?
-
这是一种非常不可读和不可调试的做事方式。为什么不在输入端解码所有输入,将所有内容(包括
%格式)处理为 Unicode,然后在输出端进行编码,而不是到处进行解码和编码? -
你不需要解码,然后编码你的字符串模板(带有
%s占位符的字符串);它已经只是 ASCII。
标签: python