在Python中使用file

在Python中使用file

本文介绍了在Python中使用file.write写入文件时出错。 UnicodeEncodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have never dealt with encoding and decoding strings, so I am quite the newbie on this front. I am receiving a UnicodeEncodeError when I try to write the contents I read from another file to a temporary file using file.write in Python. I get the following error:

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

这是我在代码中所做的事情。我正在读取XML文件,并从 mydata标签获取文本。然后,我遍历mydata以查找CDATA

Here is what I am doing in my code. I am reading an XML file and getting the text from the "mydata" tag. I then iterate through mydata to look for 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()

当我点击以下行时,它将失败并出现UnicodeEncodeError:

It fails with the UnicodeEncodeError when I hit the following line:

file.write(myCode + "\n")

我应该如何正确地对此进行编码和解码?

How should I properly encode and decode this?

推荐答案

Python2.7的 open 函数不能像python3那样透明地处理Unicode字符。关于,但是如果您想直接编写unicode字符串而不解码它们,则可以试试这个

Python2.7's open function does not transparently handle unicode characters like python3 does. There is extensive documentation on this, but if you want to write unicode strings directly without decoding them, you can try this

>>> import codecs
>>> f = codecs.open(filename, 'w', encoding='utf8')
>>> f.write(u'\u201c')

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

For comparison, this is how the error happen

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

这篇关于在Python中使用file.write写入文件时出错。 UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:51