在python中将unicode utf-8数据写入文件时出现问题
在Python程序中将unicode写入文件时遇到一些问题 以下是“保存”文件的代码:在python中将unicode utf-8数据写入文件时出现问题,python,unicode,utf-8,Python,Unicode,Utf 8,在Python程序中将unicode写入文件时遇到一些问题 以下是“保存”文件的代码: def file_save(self): # save changes to existing file if self.filename and isfile(self.filename): self.watcher.removePath(self.filename) s = codecs.open(self.filename,'w','utf-8')
def file_save(self):
# save changes to existing file
if self.filename and isfile(self.filename):
self.watcher.removePath(self.filename)
s = codecs.open(self.filename,'w','utf-8')
s.write(unicode(self.ui.editor_window.toPlainText()))
s.close()
self.ui.button_save.setEnabled(False)
self.watcher.addPath(self.filename)
# save a new file
else:
fd = QtGui.QFileDialog(self)
newfile = fd.getSaveFileName()
if newfile:
s = codecs.open(newfile,'w','utf-8')
s.write(unicode(self.ui.editor_window.toPlainText()))
s.close()
self.ui.button_save.setEnabled(False)
调用该方法后,我将收到以下错误消息:
line 113, in file_save
s.write(unicode(self.ui.editor_window.toPlainText()))
NameError: global name 'unicode' is not defined
我正在运行Python3.2,似乎在任何地方都找不到这个问题 Unicode支持在3.x中是“固定”的。普通字符串文本存储为Unicode,普通open()
函数获得了一个encoding
参数,从而使codecs.open()
过时
s = open(self.filename, 'w', encoding='utf-8')
s.write(self.ui.editor_window.toPlainText())
Unicode支持在3.x中是“固定”的。普通字符串文本存储为Unicode,普通open()
函数获得了一个encoding
参数,从而使codecs.open()
过时
s = open(self.filename, 'w', encoding='utf-8')
s.write(self.ui.editor_window.toPlainText())