我在读取文件,处理其字符串并保存为UTF-8文件时遇到问题.
这是代码:
try:
filehandle = open(filename,"r")
except:
print("Could not open file " + filename)
quit()
text = filehandle.read()
filehandle.close()
Run Code Online (Sandbox Code Playgroud)
然后我对变量文本进行一些处理.
然后
try:
writer = open(output,"w")
except:
print("Could not open file " + output)
quit()
#data = text.decode("iso 8859-15")
#writer.write(data.encode("UTF-8"))
writer.write(text)
writer.close()
Run Code Online (Sandbox Code Playgroud)
这完全输出文件,但根据我的编辑,它在iso 8859-15中这样做.由于同一编辑器将输入文件(在变量文件名中)识别为UTF-8,我不知道为什么会发生这种情况.就我的研究表明,评论的行应该可以解决问题.但是,当我使用这些行时,生成的文件主要是特殊字符的乱码,带有波浪号的文字是西班牙语.因为我难过,我真的很感激任何帮助....