【发布时间】:2015-01-27 20:10:24
【问题描述】:
大家好,
我想向网络应用发送一个字符串变量。通常,当我在代码的“开关”部分手动创建它时,我可以毫无问题地发送一个字符串。但是当我从函数中获取该字符串时,我得到了一个错误。我的意思是,如果 counterstring='12.3' ,没有问题。但是如果counterstring=ReadCounter(),就有问题了。
这是代码的实际部分:
import cherrypy
import webbrowser
import os
import json
import sys
MEDIA_DIR = os.path.join(os.path.abspath("."), u"media")
class AjaxApp(object):
@cherrypy.expose
def index(self):
return open(os.path.join(MEDIA_DIR, u'index.html'))
@cherrypy.expose
def switch(self):
counterstring=ReadCounter()
return counterstring
config = {'/media':
{'tools.staticdir.on': True,
'tools.staticdir.dir': MEDIA_DIR,
}
}
def open_page():
webbrowser.open("http://127.0.0.1:8080/")
cherrypy.engine.subscribe('start', open_page)
cherrypy.tree.mount(AjaxApp(), '/', config=config)
cherrypy.engine.start()
错误是:
ERROR:cherrypy.error.55086800:[27/Jan/2015:02:47:09] Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 589, in run
self.respond(pi)
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 690, in respond
self.handle_error()
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 767, in handle_error
self.error_response()
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 401, in set_response
message=self._message)
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 407, in get_error_page
return get_error_page(*args, **kwargs)
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 535, in get_error_page
return result.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 2271: ordinal not in range(128)
ERROR:cherrypy.error.42068624:[29/Jan/2015:09:14:46] Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 589, in run
self.respond(pi)
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 690, in respond
self.handle_error()
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 767, in handle_error
self.error_response()
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 402, in set_response
message=self._message)
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 408, in get_error_page
return get_error_page(*args, **kwargs)
File "C:\Python27\lib\site-packages\cherrypy\_cperror.py", line 536, in get_error_page
return result.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 2294: ordinal not in range(128)
我尝试了很多关于编码/解码的事情。我在应用程序中为字符串添加了编码/解码函数
我有 # -- 编码:utf-8 -- 在我的代码顶部。
能否请您谈谈解决此问题的建议?
提前致谢。
【问题讨论】:
-
什么是“反串”? ASCII 格式的字符串?
-
counterstrıng 是在该行创建的字符串。它必须是 utf-8,因为顶部有声明。
标签: python unicode decode encode cherrypy