これを複製としてマークする前に、私はfrom __future__ import unicode_literals
をすべてに使用することによって、ファイルの先頭に# -*- coding: utf-8 -*-
とすると、str.encode('utf8')
とstr.decode('utf8')
の組み合わせと組み合わせはありません。できるだけ具体的になるように何かが間違っていることは知っています。辞書をJSON Array/Objectに変換し、それをWebページ上の生の文字列形式で表示しています。'utf8'コーデックは、位置0のバイト0xb5をデコードできません:無効な開始バイト
私が発行しているユニコード文字列は、ファイル名に「μ」で始まる文字列なので、下のコードの最後の4行目にエラーが発生します。配列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
ことができますトレースバックを追加してください。 – snakecharmerb
また、 'b64'関数はどこから来ますか?そのソースコードはありますか? – snakecharmerb
@snakecharmerb追加され、b64関数はbase64.b64encode()関数です。 –