Ich wurde in Python den folgenden Code ausführen:Python wirft ascii-Codec kann nicht codieren, wenn das Parsen von XML
import xml.etree.ElementTree as ET
tree = ET.parse('dplp_11.xml')
root = tree.getroot()
f = open('workfile', 'w')
for country in root.findall('article'):
rank = country.find('year').text
name = country.find('title').text
if(int(rank)>2009):
f.write(name)
auth = country.findall('author')
for a in auth:
#print str(a)
f.write(a.text)
f.write(',')
f.write('\n')
Ich habe einen Fehler:
Traceback (most recent call last):
File "parser.py", line 14, in <module>
f.write(a.text)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 4: ordinal not in range(128)
Ich habe versucht, die Daten zu analysieren DBLP Das sieht so aus:
<?xml version="1.0"?>
<dblp>
<article mdate="2011-01-11" key="journals/acta/Saxena96">
<author>Sanjeev Saxena</author>
<title>Parallel Integer Sorting and Simulation Amongst CRCW Models.</title>
<pages>607-619</pages>
<year>1996</year>
<volume>33</volume>
<journal>Acta Inf.</journal>
<number>7</number>
<url>db/journals/acta/acta33.html#Saxena96</url>
<ee>http://dx.doi.org/10.1007/BF03036466</ee>
</article>
<article mdate="2015-07-14" key="journals/acta/BozapalidisFR12">
<author>Symeon Bozapalidis</author>
<author>Zoltán Fülöp 0001</author>
<author>George Rahonis</author>
<title>Equational weighted tree transformations.</title>
<pages>29-52</pages>
<year>2012</year>
<volume>49</volume>
<journal>Acta Inf.</journal>
<number>1</number>
<ee>http://dx.doi.org/10.1007/s00236-011-0148-5</ee>
<url>db/journals/acta/acta49.html#BozapalidisFR12</url>
</article>
</dblp>
Wie kann ich es lösen?
Beachten Sie, dass es das 'f.write()' Linie ist, die die Ausnahme auslöst. Es ist nicht das * XML-Parsing *, das hier auftritt, sondern das Schreiben in die Textdatei, die das Problem verursacht. 'f.write (u'Zolt \ xe1n ')' würde Ihnen genau den gleichen Fehler geben. –