【发布时间】:2015-08-27 14:21:40
【问题描述】:
我有一个 python 脚本,它生成一个 json 文件,该文件进一步用于 C# 应用程序。这是创建导出的代码:
def put(data, filename):
with open(filename, 'w') as outfile:
json.dump(data, outfile, indent=4, ensure_ascii=False, encoding='utf8')
然后在 C# 应用程序中,我尝试通过以下方式读取它:
public static string readFile(string filename)
{
using (StreamReader r = new StreamReader(filename))
{
return r.ReadToEnd();
}
}
//different file
string json_result = funcs.readFile(out_file);
VirtualMachine newvm = JsonConvert.DeserializeObject<MyClass>(json_result);
问题是 json_result 在每个换行符处包含字符 \r\n 并且无法反序列化 json。
我试图检查python脚本产生的编码,根据SublimeText是u'Undefined'
如何让 python 为 C# 正确编码文件或让 C# 以正确的编码加载它?
【问题讨论】:
标签: c# python json readfile file-encodings