我正在尝试创建一个以UTF-8编码的C ++文件。实际上,当我尝试创建这样的文件时:
#include <fstream>
#include <iostream>
int main() {
std::ofstream myfile("test.json");
myfile << "{\"test\":\"é\"}";
myfile.close();
}
然后python无法将其解码为UTF-8,我必须使用ISO-8859-1对其进行解码:
import json
import codecs
with codecs.open('test.json', encoding='ISO-8859-1') as json_file:
data = json.load(json_file)
print(data)
并且当我将编码设置为utf8时,出现错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 0: unexpected end of data
如何使该文件使用C ++进行UTF-8编码?我真的需要此文件为UTF-8,而不是ISO-8859-1。
我还尝试使用Vim(而不使用c ++)创建json,并且有效。
您需要配置文本编辑器/ IDE。