Problem writing unicode utf-8 data to file in python
I'm having some issues writing unicode to a file in my Python program.
Here is the code that 'saves' the file:
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(se开发者_运维知识库lf.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)
Once that method is called I receive this error message:
line 113, in file_save
s.write(unicode(self.ui.editor_window.toPlainText()))
NameError: global name 'unicode' is not defined
I am running Python 3.2 and can't seem to find the issue anywhere.
Unicode support was "fixed" in 3.x. Normal string literals are stored as Unicode, and the normal open()
function has gained an encoding
argument thereby making codecs.open()
obsolete.
s = open(self.filename, 'w', encoding='utf-8')
s.write(self.ui.editor_window.toPlainText())
精彩评论