Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将unicode utf-8数据写入文件时出现问题_Python_Unicode_Utf 8 - Fatal编程技术网

在python中将unicode utf-8数据写入文件时出现问题

在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')

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