'utf8' 编解码器无法解码字节 0xf3

'utf8' codec can't decode byte 0xf3

我正在使用 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')

但是我得到了以下错误。

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 72: invalid
continuation byte

您的文件未使用 UTF-8 编码,错误出现在 fp.read() 行。您必须使用:

import io
io.open(filename, encoding='latin-1')

加入路径的正确用法是:

os.path.join(root, f)