kylin Linux环境python读json文件报错UTF-8 BOM
报错描述:
windows环境下,python代码读取json文件正常,但是sftp到linux环境下
报错信息:
json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
解决办法:
新建一个 remove_pom.py ,生成一个 utf8 without BOM 文件
import codecs
input_file_path = 'config json.example' # 输入文件路径
output_file_path = 'config.json' # 输出文件路径
# 打开原始文件并跳过BOM
with codecs.open(input_file_path, 'r', encoding='utf-8-sig') as source_file:
content = source_file.read()
# 将内容写入新文件,此时不会包含BOM
with open(output_file_path, 'w', encoding='utf-8') as target_file:
target_file.write(content)