`gbk` codec can‘t encode character `\u2764` in position 5438: illegal multibyte sequence

我将字典打包为json文件代码:

with open(save_path, "w") as f:
	json_str = json.dumps(data_dic, ensure_ascii=False, indent=4, separators=(',', ':'))
	f.write(json_str)

字典中有中文,故而指定 ensure_ascii=False, 但是会报错:

UnicodeEncodeError: 
'gbk' codec can't encode character '\u2764' in position 5438: illegal multibyte sequence

原因是win10会指定默认编码为GBK,故而可以使用UTF-8编码解决

with open(save_path, "w", encoding='utf-8') as f:

你可能感兴趣的:(`gbk` codec can‘t encode character `\u2764` in position 5438: illegal multibyte sequence)