我正在製作一個算法來分類單詞的次數,它們出現在由文件給出的文本中。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