jizhanlong 发表于 2011-06-14 08:49

UnicodeDecodeError 急需各位帮忙

UnicodeDecodeError at /supe/resolveTask/newSample/1,/
'utf8' codec can't decode byte 0xa4 in position 945: unexpected code byteRequest Method: GET
Request URL: http://127.0.0.1:8000/supe/resolveTask/newSample/1,/
Django Version: 1.2.3
Exception Type: UnicodeDecodeError
Exception Value: 'utf8' codec can't decode byte 0xa4 in position 945: unexpected code byte
Exception Location: D:\Python25\lib\encodings\utf_8.py in decode, line 16
Python Executable: D:\Python25\python.exe
Python Version: 2.5.4
Python Path: ['E:\\supe', 'C:\\windows\\system32\\python25.zip', 'D:\\Python25\\DLLs', 'D:\\Python25\\lib', 'D:\\Python25\\lib\\plat-win', 'D:\\Python25\\lib\\lib-tk', 'D:\\Python25', 'D:\\Python25\\lib\\site-packages', 'D:\\Python25\\lib\\site-packages\\win32', 'D:\\Python25\\lib\\site-packages\\win32\\lib', 'D:\\Python25\\lib\\site-packages\\Pythonwin', 'D:\\Python25\\lib\\site-packages\\wx-2.9.1-msw']
Server time: Mon, 13 Jun 2011 17:39:33 +0800


Unicode error hint
The string that could not be encoded/decoded was: ????1?7 //1

Traceback:
File "D:\Python25\Lib\site-packages\django\core\handlers\base.py" in get_response
100.                     response = callback(request, *callback_args, **callback_kwargs)
File "E:\supe\..\supe\resolveTask\views.py" in newSample
524.         return render_to_response('resolveTask/newSample.htm', {'action':'/supe/resolveTask/newSampleSubmit/', 'type':type, 'tasklist':tasklist, 'selectedTaskName':selectedTaskName, 'samplelist':samplelist, 'sampleID':sampleID, 'selectedSampleID':selectedSampleID, 'runMode':runMode, 'startSupeServerFlag':startSupeServerFlag, 'processes':processes, 'runTimes':runTimes, 'deployDir':deployDir, 'setNodeFlag':setNodeFlag, 'node':node, 'port':port, 'lines':lines, 'bSampleNotExist':bSampleNotExist})
File "D:\Python25\Lib\site-packages\django\shortcuts\__init__.py" in render_to_response
20.   return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "D:\Python25\Lib\site-packages\django\template\loader.py" in render_to_string
181.         t = get_template(template_name)
File "D:\Python25\Lib\site-packages\django\template\loader.py" in get_template
157.   template, origin = find_template(template_name)
File "D:\Python25\Lib\site-packages\django\template\loader.py" in find_template
134.             source, display_name = loader(name, dirs)
File "D:\Python25\Lib\site-packages\django\template\loader.py" in __call__
42.         return self.load_template(template_name, template_dirs)
File "D:\Python25\Lib\site-packages\django\template\loader.py" in load_template
45.         source, display_name = self.load_template_source(template_name, template_dirs)
File "D:\Python25\Lib\site-packages\django\template\loaders\filesystem.py" in load_template_source
39.                     return (file.read().decode(settings.FILE_CHARSET), filepath)
File "D:\Python25\lib\encodings\utf_8.py" in decode
16.   return codecs.utf_8_decode(input, errors, True)

Exception Type: UnicodeDecodeError at /supe/resolveTask/newSample/1,/
Exception Value: 'utf8' codec can't decode byte 0xa4 in position 945: unexpected code byte
哪位帮忙看一下   这个问题困扰好几天了    再搞不定就会出问题了   望各位多帮忙

iamacnhero 发表于 2011-06-14 09:39

还是字符编码的问题。

你先用type(variale)看一下变量的类型,是unicode还是str.
最好帖些代码上来

ccporxy 发表于 2011-06-14 09:53

File "D:\Python25\Lib\site-packages\django\template\loaders\filesystem.py" in load_template_source
39.                     return (file.read().decode(settings.FILE_CHARSET), filepath)
File "D:\Python25\lib\encodings\utf_8.py" in decode
16.   return codecs.utf_8_decode(input, errors, True)

栈回退显示编码问题

0xa4 不是有效的utf-8字符,确定你的文档是按照utf-8编码或者兼容编码保存,否则就要转换。
话说你在做繁体开发?

jizhanlong 发表于 2011-06-14 13:39

何以见得是繁体   我是想把linux上的一个程序移植到windows上    我出错的这个view肯定是utf8的我拿Ultra Edit 转换过   不过其他的被调用的模块就不敢保证了   我下午全转了试试

ccporxy 发表于 2011-06-14 13:48

你转下这个脚本文件 应该就ok了
resolveTask/newSample.htm
不过不排除其他的也有问题
最好的方法就是 settings.FILE_CHARSET 设置文本保存的编码

jizhanlong 发表于 2011-06-14 17:54

多谢各位帮忙   问题终于解决了   问题就出在那个htm上   重新转码一下就行了
请教一下ccporxy    你说的那个最好的方法不太明白    麻烦解说一下   谢了

ccporxy 发表于 2011-06-15 09:14

django配置说明:
https://docs.djangoproject.com/en/1.0/ref/unicode/#ref-unicode

Don’t be fooled into thinking that if your DEFAULT_CHARSET setting is set to something
other than 'utf-8' you can use that other encoding in your bytestrings!
DEFAULT_CHARSET only applies to the strings generated as the result of template rendering (and e-mail).
Django will always assume UTF-8 encoding for internal bytestrings. The reason for this is that the DEFAULT_CHARSET
setting is not actually under your control (if you are the application developer). It’s under the control of the person
installing and using your application – and if that person chooses a different setting, your code must still continue to
work. Ergo, it cannot rely on that setting.

这里看来使用utf-8还是最好的选择。
页: [1]
查看完整版本: UnicodeDecodeError 急需各位帮忙