Raj*_*jiv 3 python json character-encoding
我使用python 2.7来读取json文件.我的代码是
import json
from json import JSONDecoder
import os
path=os.path.dirname(os.path.abspath(__file__))+'/json'
print path
for root, dirs, files in os.walk(os.path.dirname(path+'/json')):
for f in files:
if f.lower().endswith((".json")):
fp=open(root + '/'+f)
data = fp.read()
print data.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
但是我得到了以下错误.
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 72: invalid
continuation byte
Run Code Online (Sandbox Code Playgroud)
Arc*_*s B 10
您的文件未以UTF-8编码,并且错误发生在该fp.read()行.你必须使用:
import io
io.open(filename, encoding='latin-1')
Run Code Online (Sandbox Code Playgroud)
加入路径的正确而非平台依赖性用法是:
os.path.join(root, f)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13508 次 |
| 最近记录: |