-1

临时XML

<?xml version="1.0" encoding="utf-8"?>
<PubmedArticleSet>
    <LastName>Nalivaĭko</LastName>
    <ForeName>Anthony V</ForeName>
</PubmedArticleSet>

我的代码

import xml.dom.minidom


doc = xml.dom.minidom.parse("temp.xml");
file = open('output1.xml','w')

articles = doc.getElementsByTagName('PubmedArticleSet')
for art in articles:
    ln = art.getElementsByTagName("LastName")[0]
    data = ln.firstChild.nodeValue
    file.write("<LastName>")
    file.write(data)
    file.write("</LastName>\n")
print("Completed")
file.close()

我需要输出与 LastName 标记中的字符串相同。

所需输出- <LastName>Nalivaĭko</LastName>

运行代码时出现此错误

Traceback (most recent call last):
  File "C:\Users\Yugam\Desktop\python\ParsingUsingDOM.py", line 12, in <module>
    file.write(data)
  File "C:\Users\Yugam\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u012d' in position 6: character maps to <undefined>
4

1 回答 1

0

您可以使用所需的编码打开文件进行写入,如下所示:

open('output1.xml','w', encoding='utf-8')

然后你可以像往常一样写出你的 unicode 字符串。

输出文件:

<LastName>Nalivaĭko</LastName>
于 2018-11-26T09:08:44.873 回答