我正在制作一个算法来分类单词的次数,它们出现在由文件给出的文本中。Python:发布打印到文件特殊字符(西班牙文字母)
有我的方法:
def printToFile(self, fileName):
file_to_print = open(fileName, 'w')
file_to_print.write(str(self))
file_to_print.close()
且有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')
当我通过控制台打印数据没有问题,不过,当我通过文件做随机出现字符。
This is should be the output to the file
如果我这样做会发生这样的:“UnicodeEncodeError:'ascii'编解码器无法编码字符u'\ xc3'在位置22:序号不在范围内(128)“在”file_to_print.write(str(self) )“ – Alkesst
重要的问题 - 你在什么版本? –
我现在正在使用python 2.7 – Alkesst