我在Linux上,想要将字符串(在utf-8中)写入txt文件.这是我的代码:
# -*- 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()
Run Code Online (Sandbox Code Playgroud)
当我在Windows上时,它可以工作.但在Linux上,我收到此错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position in position 36: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?谢谢.
您可以尝试使用"编解码器"模块:
import codecs
with codecs.open('filename', 'w', encoding='utf-8') as out:
out.write(u'some text')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4825 次 |
| 最近记录: |