我在Linux上并希望将字符串(在utf-8中)写入txt文件。这是我的代码:python - 将非ascii字符写入文件
# -*- coding: UTF-8-*-
import os
import sys
def __init__(self, dirname, speaker, file, exportFile):
text_file = open(exportFile, "a")
text_file.write(speaker.encode("utf-8"))
text_file.write(file.encode("utf-8"))
text_file.close()
当我在Windows上,它的工作原理。但在Linux上,我得到这个错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position in position 36: ordinal not in range(128)
我该如何解决这个问题?谢谢。
你确定你不想'解码(“utf-8”)'你的UTF8字符串转换为字节串? – mgilson
你有一个样本或链接到你的来源? – alvas
你有没有尝试在'“au”模式下打开文件? –