[英]Python: Issue printing to file special characters (Spanish alphabet)
I'm making an an algorithm to classify words with the number of times they appear in a text given by a file. 我正在制定一种算法,可以根据单词在文件给定的文本中出现的次数对单词进行分类。
There is my method: 有我的方法:
def printToFile(self, fileName):
file_to_print = open(fileName, 'w')
file_to_print.write(str(self))
file_to_print.close()
and there is the str: 并且有str:
def __str__(self):
cadena = ""
self.processedWords = collections.OrderedDict(sorted(self.processedWords.items()))
for key in self.processedWords:
cadena += str(key) + ": " + str(self.processedWords[key]) + "\n"
return cadena.decode('string_escape')
When I print the data through console there is no issues, nevertheless, when I do through file appears random characters. 但是,当我通过控制台打印数据时,没有问题,但是当我通过文件进行操作时,出现了随机字符。
这看起来像是一个编码问题,请尝试按以下方式打开文件:open(“ file name”,“ w”,encoding =“ utf8”)Utf8是最受欢迎的编码,但它可能不是真正的编码,您可能必须查看其他编码,例如utf16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.