[error] json file 不能读取的解决办法 TypeError: Input string must be text, not bytes

最开始是用

with open(path) as f:
	data = json.loads(f)

然后报错TypeError: Input string must be text, not bytes

尝试1,加上解码:

with open(path, encoding='utf-8') as f:
	data = json.loads(f)

仍报错TypeError: Input string must be text, not bytes

尝试2,读取每行:

with open(path, encoding='utf-8') as f:
	data = [json.loads(line) for line in f]

报错JSONDecodeError: Expecting property name enclosed in double quotes or '}'

这时候,我们知道,肯定是json file里面有些“非法”字符需要删除。

最终尝试,使用nested replace去除“非法”字符:

# nested replace() 
with open(gentle_file, encoding='utf-8') as f:
    tmp = f.read().replace('\t','').replace('\n','').replace(',}','}').replace(',]',']')
    data = json.loads(tmp)

成功!

你可能感兴趣的:(error,python,json)