在Python中使用file.write写入文件时出错.UnicodeEncodeError

use*_*864 11 unicode encode decode fwrite python-2.7

我从来没有处理过编码和解码字符串,所以我就是这方面的新手.当我尝试使用Python中的file.write将我从另一个文件读取的内容写入临时文件时,我收到了一个UnicodeEncodeError.我收到以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 41333: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

这是我在我的代码中所做的.我正在读取XML文件并从"mydata"标签获取文本.然后我遍历mydata寻找CDATA

    parser = etree.XMLParser(strip_cdata=False)
    root = etree.parse(myfile.xml, parser)
    data = root.findall('./mydata')
    # iterate through list to find text (lua code) contained in elements containing CDATA
    for item in myData:
        myCode = item.text

    # Write myCode to a temporary file.
    tempDirectory = tempfile.mkdtemp(suffix="", prefix="TEST_THIS_")
    file = open(tempDirectory + os.path.sep + "myCode.lua", "w")

    file.write(myCode + "\n")
    file.close()
Run Code Online (Sandbox Code Playgroud)

当我点击以下行时,它失败了UnicodeEncodeError:

file.write(myCode + "\n")
Run Code Online (Sandbox Code Playgroud)

我应该如何正确编码和解码?

met*_*ter 24

Python2.7的open函数不透明地处理像python3那样的unicode字符.有这个广泛的文档,但如果你想直接写unicode字符串解码没有他们,你可以试试这个

>>> import codecs
>>> f = codecs.open(filename, 'w', encoding='utf8')
>>> f.write(u'\u201c')
Run Code Online (Sandbox Code Playgroud)

为了比较,这就是错误发生的方式

>>> f = open(filename, 'w')
>>> f.write(u'\u201c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)