python3 写入文件 中文乱码

上一篇 / 下一篇  2018-01-17 11:25:07 / 个人分类:python

代码为:
defsendtext(self):
LOG.info("send text")
text =self.edittext.toPlainText()
# text = "你好"
js_text="var InputMethod = require(\"yunos/system/InputMethod\").getInstance();InputMethod.commitString(\"%s\");process.exit(0);"%text
print(type(js_text))
js_text=js_text.encode("utf8")
withopen("./res/js/sendtext.js","wb")asf:
f.write(js_text)
self.__A.push("./res/js/sendtext.js","/data/")
self.__A.shell2("node --harmony --output-console /data/sendtext.js")
在使用pyqt的时候,获取一个Qedittext的值。是中文。
带入后,js_text print的时候就是中文,但是,写入的时候,怎么样都是乱码。

如果不给js_text转码的话,
提示TypeError: 'str' does not support the buffer interface

首先要给js_text转码
然后再打开文件的时候是wb

然后就可以很完美的显示中文。

TAG: python3

 

评分:0

我来说两句

Open Toolbar