'utf8'编解码器无法解码位置0中的字节0xb5:无效的起始字节

时间:2017-01-04 19:09:31

标签: python json django python-2.7 unicode

在将此标记为重复之前,我想明确表示我已经尝试了无数的解决方案,通过from __future__ import unicode_literals使用str.encode('utf8') str.decode('utf8')和{{1}的每个方法和组合来消除此问题将# -*- coding: utf-8 -*-放在文件的开头,什么不是。我知道我出错了所以我会尽可能具体,我将字典转换为JSON数组/对象并在网页上以原始字符串形式显示它。

我遇到的unicode字符串是文件名中以“μ”开头的字符串,因此错误发生在下面代码的最后四行。 files数组将该字符串索引处的值显示为 \ xb5Torrent.lnk

if os.path.isdir(finalDirPath):
        print "is Dir"
        for (path,dir,files) in os.walk(finalDirPath):

            if dir!=[]:
                for i in dir:
                    if not hidden(os.path.join(path,i)):
                        # Here
                        JSONarray.append({"ext":"dir","path":b64(os.path.join(path,i)),"name":i})

            if files!=[]:
                for i in files:
                    if not hidden(os.path.join(path,i)):
                        # Here
                        JSONarray.append({"ext":i.split('.')[-1],"path":b64(os.path.join(path,i)),"name":i})
            break
        jsonStr = {"json":json.dumps(JSONarray)}
        return render(request,"json.html",jsonStr)

这是追溯:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\ICT\Other\Python\Django\trydjango18\src\newsletter\views.py", line 468, in getJSON
JSONarray.append({"ext":i.split('.')[-1],"path":b64(os.path.join(path.encode('utf8'),i)),"name":i})
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb5 in position 0: invalid start byte

1 个答案:

答案 0 :(得分:4)

一个演示您问题的简短示例:

>>> json.dumps('\xb5Torrent.lnk')

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    json.dumps('\xb5Torrent.lnk')
  File "C:\Python27\lib\json\__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 201, in encode
    return encode_basestring_ascii(o)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb5 in position 0: invalid start byte

您的数组files包含字节字符串,但json.dumps()希望数据中的任何字符串都是unicode。它假设任何字节字符串都是utf-8编码但你的字符串使用不同的编码:可能是latin1或者其他可能的东西。在将文件系统添加到JSONarray结构之前,您需要找出文件系统使用的编码并将所有文件名解码为unicode。

首先要检查文件系统编码:

import sys
print sys.getfilesystemencoding()

应告诉您用于文件名的编码,然后您只需确保所有路径操作都使用unicode:

import sys
fsencoding = sys.getfilesystemencoding()
if os.path.isdir(finalDirPath):
    print "is Dir"
    for (path,dir,files) in os.walk(finalDirPath):
        path = path.decode(fsencoding)
        for i in dir:
            i = i.decode(fsencoding)
            if not hidden(os.path.join(path,i)):
                # Here
                JSONarray.append({
                    "ext": "dir",
                    "path": b64(os.path.join(path,i)),
                    "name": i})
                })

        for i in files:
            i = i.decode(fsencoding)
            if not hidden(os.path.join(path,i)):
                # Here
                JSONarray.append({
                    "ext": i.split('.')[-1],
                    "path": b64(os.path.join(path,i)),
                    "name":i
                })
        break
    jsonStr = {"json":json.dumps(JSONarray)}
    return render(request,"json.html",jsonStr)