Python读写中文变量总会有问题,尤其是读写Windows下的txt文件,如果把文件保存为UTF8格式就没问题了,如新建一个txt文件,另存为UTF8格式:

Python 如何将中文变量写入txt文件

然后运行测试的Python代码:

# -*- coding: utf-8 -*-
fd = open("./data.txt", "w")
string = raw_input()
print string
fd.write(string)
fd.close()

这时再输入中文就没问题了,可以写入txt文件了,主要是代码的编码格式要和文件一致。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
相关资源
相似解决方案