import io
def write_ngrams(table, filename):
with io.open(filename, "w") as file:
for i in table:
outputstring=(('%d %s\n' % (table[i], i)))
encoded = outputstring.encode("utf-8")
file.write(encoded)
tabel = ngram_table('hiep, hiep, hoera!', 3, 0) // these are not really interesting for now
write_ngrams(tabel, "testfile3.txt")
我在 file.write(encoded) 行收到一个错误,指出以下内容:
TypeError: write() argument must be str, not bytes.
但是我的任务是:输出必须使用 utf8 编码,
这意味着输出应该是 b'....' 的形式
通过我尝试过的方法,我只能得到没有编码或错误的字符串。但是,当我使用 print(encoded) 时,我确实收到了 UTF-8 编码的输出,但是当我将其写入文件时,编码消失了或者出现错误。
任何提示将不胜感激。
您可以将字符串传递给write()
& 打开文件,编码设置为utf-8
import io
def write_ngrams(table, filename):
with io.open(filename, "w", encoding='utf-8') as file:
for i in table:
outputstring=(('%d %s\n' % (table[i], i)))
file.write(outputstring)
tabel = ngram_table('hiep, hiep, hoera!', 3, 0) // these are not really interesting for now
write_ngrams(tabel, "testfile3.txt")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句