0

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(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)

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.

1 Answer 1

5

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.