Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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抛出ascii编解码器无法编码u'\x';第108位:解析xml时序号不在范围(128)内错误_Python_Xml - Fatal编程技术网

Python抛出ascii编解码器无法编码u'\x';第108位:解析xml时序号不在范围(128)内错误

Python抛出ascii编解码器无法编码u'\x';第108位:解析xml时序号不在范围(128)内错误,python,xml,Python,Xml,我用Python运行了以下代码: import xml.etree.ElementTree as ET tree = ET.parse('dblp_140.xml') root = tree.getroot() f = open('hi', 'w') for country in root.findall('article'): rank = country.find('year').text name = country.find('title') if(int(ra

我用Python运行了以下代码:

import xml.etree.ElementTree as ET
tree = ET.parse('dblp_140.xml')
root = tree.getroot()
f = open('hi', 'w')
for country in root.findall('article'):
    rank = country.find('year').text
    name = country.find('title')

    if(int(rank)>2009):
        f.write(name.text)
        f.write(':')
        auth = country.findall('author')
        for a in auth:
            #print str(a)
            f.write(a.text.encode('utf8'))  
            f.write(',')
        f.write('\n')
我在位置108:ordinal不在范围(128)中遇到一个错误,ascii编解码器无法对u'\x'进行编码。我试图分析dblp数据,如下所示:

<?xml version="1.0" encoding="iso-8859-1"?>
<dblp>
<article mdate="2011-08-25" key="journals/envsoft/ElbernSTE00">
<author>Hendrik Elbern</author>
<author>H. Schmidt</author>
<author>O. Talagrand</author>
<author>Adolf Ebel</author>
<title>4D-variational data assimilation with an adjoint air quality model for emission analysis.</title>
<pages>539-548</pages>
<year>2000</year>
<volume>15</volume>
<journal>Environmental Modelling and Software</journal>
<number>6-7</number>
<url>db/journals/envsoft/envsoft15.html#ElbernSTE00</url>
<ee>http://dx.doi.org/10.1016/S1364-8152(00)00049-9</ee>
</article>
<article mdate="2015-01-12" key="journals/envsoft/VerstegenKHF14">
<author>Judith Anne Verstegen</author>
<author>Derek Karssenberg</author>
<author>Floor van der Hilst</author>
<author>André P. C. Faaij</author>
<title>Identifying a land use change cellular automaton by Bayesian data assimilation.</title>
<pages>121-136</pages>
<year>2014</year>
<volume>53</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2013.11.009</ee>
<url>db/journals/envsoft/envsoft53.html#VerstegenKHF14</url>
</article>
<article mdate="2014-07-15" key="journals/envsoft/FechtBB14">
<author>Daniela Fecht</author>
<author>Linda Beale</author>
<author>David Briggs</author>
<title>A GIS-based urban simulation model for environmental health analysis.</title>
<pages>1-11</pages>
<year>2014</year>
<volume>58</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2014.03.013</ee>
<url>db/journals/envsoft/envsoft58.html#FechtBB14</url>
</article>
<article mdate="2008-03-03" key="journals/envsoft/GhenuRS08">
<author>A. Ghenu</author>
<author>J.-M. Rosant</author>
<author>J.-F. Sini</author>
<title>Dispersion of pollutants and estimation of emissions in a street canyon in Rouen, France.</title>
<pages>314-321</pages>
<year>2008</year>
<volume>23</volume>
<journal>Environmental Modelling and Software</journal>
<number>3</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2007.05.017</ee>
<url>db/journals/envsoft/envsoft23.html#GhenuRS08</url>
</article>
<article mdate="2014-07-15" key="journals/envsoft/ChaS14">
<author>YoonKyung Cha</author>
<author>Craig A. Stow</author>
<title>A Bayesian network incorporating observation error to predict phosphorus and chlorophyll a in Saginaw Bay.</title>
<pages>90-100</pages>
<year>2014</year>
<volume>57</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2014.02.010</ee>
<url>db/journals/envsoft/envsoft57.html#ChaS14</url>
</article>
<article mdate="2015-04-10" key="journals/envsoft/PinetDS07">
<author>François Pinet</author>
<author>Magali Duboisset</author>
<author>Vincent Soulignac</author>
<title>Using UML and OCL to maintain the consistency of spatial data in environmental information systems.</title>
<pages>1217-1220</pages>
<year>2007</year>
<volume>22</volume>
<journal>Environmental Modelling and Software</journal>
<number>8</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2006.10.003</ee>
<url>db/journals/envsoft/envsoft22.html#PinetDS07</url>
</article>
<article mdate="2015-01-13" key="journals/envsoft/CastronovaG13">
<author>Anthony M. Castronova</author>
<author>Jonathan L. Goodall</author>
<title>Simulating watersheds using loosely integrated model components: Evaluation of computational scaling using OpenMI.</title>
<pages>304-313</pages>
<year>2013</year>
<volume>39</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2012.01.020</ee>
<url>db/journals/envsoft/envsoft39.html#CastronovaG13</url>
</article>
<article mdate="2005-10-28" key="journals/envsoft/BishopHS05">
<author>Ian D. Bishop</author>
<author>R. Bruce Hull IV</author>
<author>Christian Stock</author>
<title>Supporting personal world-views in an envisioning system.</title>
<pages>1459-1468</pages>
<year>2005</year>
<volume>20</volume>
<journal>Environmental Modelling and Software</journal>
<number>12</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2004.06.014</ee>
<url>db/journals/envsoft/envsoft20.html#BishopHS05</url>
</article>
<article mdate="2015-01-12" key="journals/envsoft/LaucelliBG12">
<author>Daniele Laucelli</author>
<author>Luigi Berardi</author>
<author>Orazio Giustolisi</author>
<title>Assessing climate change and asset deterioration impacts on water distribution networks: Demand-driven or pressure-driven network modeling?</title>
<pages>206-216</pages>
<year>2012</year>
<volume>37</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2012.04.004</ee>
<url>db/journals/envsoft/envsoft37.html#LaucelliBG12</url>
</article>
<article mdate="2005-10-28" key="journals/envsoft/GeMSH05">
<author>Ying Ge</author>
<author>Doug MacDonald</author>
<author>Sébastien Sauvé</author>
<author>William Hendershot</author>
<title>Modeling of Cd and Pb speciation in soil solutions by WinHumicV and NICA-Donnan model.</title>
<pages>353-359</pages>
<year>2005</year>
<volume>20</volume>
<journal>Environmental Modelling and Software</journal>
<number>3</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2003.12.014</ee>
<url>db/journals/envsoft/envsoft20.html#GeMSH05</url>
</article>
<article mdate="2015-06-29" key="journals/envsoft/DupasPRJDG15">
<author>Rémi Dupas</author>
<author>Virginie Parnaudeau</author>
<author>Raymond Reau</author>
<author>Marie Hélène Jeuffroy</author>
<author>Patrick Durand</author>
<author>Chantal Gascuel-Odoux</author>
<title>Integrating local knowledge and biophysical modeling to assess nitrate losses from cropping systems in drinking water protection areas.</title>
<pages>101-110</pages>
<year>2015</year>
<volume>69</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2015.03.009</ee>
<url>db/journals/envsoft/envsoft69.html#DupasPRJDG15</url>
</article>
<article mdate="2005-10-28" key="journals/envsoft/RobertsT04">
<author>Philip J. W. Roberts</author>
<author>Xiaodong Tian</author>
<title>New experimental techniques for validation of marine discharge models.</title>
<pages>691-699</pages>
<year>2004</year>
<volume>19</volume>
<journal>Environmental Modelling and Software</journal>
<number>7-8</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2003.08.005</ee>
<url>db/journals/envsoft/envsoft19.html#RobertsT04</url>
</article>
<article mdate="2007-02-12" key="journals/envsoft/ElliottT07">
<author>A. H. Elliott</author>
<author>S. A. Trowsdale</author>
<title>A review of models for low impact urban stormwater drainage.</title>
<pages>394-405</pages>
<year>2007</year>
<volume>22</volume>
<journal>Environmental Modelling and Software</journal>
<number>3</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2005.12.005</ee>
<url>db/journals/envsoft/envsoft22.html#ElliottT07</url>
</article>
<article mdate="2010-01-05" key="journals/envsoft/WegmannCMSH09">
<author>Fabio Wegmann</author>
<author>Laurent Cavin</author>
<author>Matthew MacLeod</author>
<author>Martin Scheringer</author>
<author>Konrad Hungerbühler</author>
<title>The OECD software tool for screening chemicals for persistence and long-range transport potential.</title>
<pages>228-237</pages>
<year>2009</year>
<volume>24</volume>
<journal>Environmental Modelling and Software</journal>
<number>2</number>
<ee>http://dx.doi.org/10.1016/j.envsoft.2008.06.014</ee>
<url>db/journals/envsoft/envsoft24.html#WegmannCMSH09</url>
</article>
<article mdate="2015-11-10" key="journals/envsoft/MaciejewskaJRK15">
<author>Katarzyna Maciejewska</author>
<author>Katarzyna Juda-Rezler</author>
<author>Magdalena Reizer</author>
<author>Krzysztof Klejnowski</author>
<title>Modelling of black carbon statistical distribution and return periods of extreme concentrations.</title>
<pages>212-226</pages>
<year>2015</year>
<volume>74</volume>
<journal>Environmental Modelling and Software</journal>
<ee>http://dx.doi.org/10.1016/j.envsoft.2015.04.016</ee>
<url>db/journals/envsoft/envsoft74.html#MaciejewskaJRK15</url>
</article>

亨德里克·埃尔伯恩
施密特
塔拉格兰德
阿道夫·埃贝尔
用伴随空气质量模式进行4D变分数据同化,用于排放分析。
539-548
2000
15
环境建模和软件
6-7
db/journals/envsoft/envsoft15.html#ElbernSTE00
http://dx.doi.org/10.1016/S1364-8152(00)00049-9
朱迪丝·安妮·维斯特根
德里克·卡森伯格
范德希尔斯特楼
安德烈·P·C·法伊
通过贝叶斯数据同化识别土地利用变化元胞自动机。
121-136
2014
53
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2013.11.009
db/journals/envsoft/envsoft53.html#VerstegenKHF14
丹妮拉·费希特
琳达比尔
布瑞格斯
基于GIS的环境健康分析城市模拟模型。
1-11
2014
58
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2014.03.013
db/journals/envsoft/envsoft58.html#fechtb14
A.Ghenu
罗桑特
西尼
法国鲁昂街道峡谷中污染物的扩散和排放量的估算。
314-321
2008
23
环境建模和软件
3.
http://dx.doi.org/10.1016/j.envsoft.2007.05.017
db/journals/envsoft/envsoft23.html#GhenuRS08
陈永庆
克雷格·A·斯托
结合观测误差的贝叶斯网络预测萨吉诺湾的磷和叶绿素A。
90-100
2014
57
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2014.02.010
db/journals/envsoft/envsof57.html#ChaS14
弗朗索瓦·皮内
马加里·杜波塞特
文森特·苏利尼亚克
使用UML和OCL维护环境信息系统中空间数据的一致性。
1217-1220
2007
22
环境建模和软件
8.
http://dx.doi.org/10.1016/j.envsoft.2006.10.003
db/journals/envsoft/envsof22.html#PinetDS07
安东尼·M·卡斯特罗诺娃
乔纳森·L·古德尔
使用松散集成的模型组件模拟流域:使用OpenMI评估计算比例。
304-313
2013
39
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2012.01.020
db/journals/envsoft/envsoft39.html#CastronovaG13
伊恩·D·毕肖普
R.布鲁斯·赫尔四世
基督教股票
在设想系统中支持个人世界观。
1459-1468
2005
20
环境建模和软件
12
http://dx.doi.org/10.1016/j.envsoft.2004.06.014
db/journals/envsoft/envsoft20.html#BishopHS05
丹尼尔·劳切利
路易吉·贝拉迪
奥拉齐奥·朱斯托利斯
评估气候变化和资产恶化对配水网络的影响:需求驱动还是压力驱动的网络建模?
206-216
2012
37
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2012.04.004
db/journals/envsoft/envsoft37.html#LaucelliBG12
莺歌
道格麦克唐纳
Sébastien Sauvé
威廉·亨德肖特
用WinHumicV和NICA Donnan模型模拟土壤溶液中镉和铅的形态。
353-359
2005
20
环境建模和软件
3.
http://dx.doi.org/10.1016/j.envsoft.2003.12.014
db/journals/envsoft/envsoft20.html#GeMSH05
雷米·杜帕斯
维吉尼帕诺多酒店
雷蒙德·雷乌
玛丽·Hélène Jeuffroy
帕特里克·杜兰德
尚塔尔加斯奎尔奥多克斯酒店
结合当地知识和生物物理模型,评估饮用水保护区种植制度造成的硝酸盐损失。
101-110
2015
69
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2015.03.009
db/journals/envsoft/envsoft69.html#DupasPRJDG15
菲利普·J·W·罗伯茨
田晓东
验证海洋排放模型的新实验技术。
691-699
2004
19
环境建模和软件
7-8
http://dx.doi.org/10.1016/j.envsoft.2003.08.005
db/journals/envsoft/envsof19.html#RobertsT04
艾略特
S.A.特罗斯代尔
低影响城市雨水排放模型综述。
394-405
2007
22
环境建模和软件
3.
http://dx.doi.org/10.1016/j.envsoft.2005.12.005
db/journals/envsoft/envsof22.html#elliott07
法比奥·韦格曼
劳伦特·卡文
马修·麦克劳德
马丁·谢林格
康拉德·亨格尔布勒
经合组织软件工具,用于筛选化学品的持久性和远距离运输潜力。
228-237
2009
24
环境建模和软件
2.
http://dx.doi.org/10.1016/j.envsoft.2008.06.014
db/journals/envsoft/envsof24.html#WegmannCMSH09
卡塔兹娜·马西耶夫斯卡
卡塔兹娜·朱达·雷兹勒
玛格达莱娜·瑞泽
Krzysztof Klejnowski
黑碳统计分布和极端浓度重现期的建模。
212-226
2015
74
环境建模和软件
http://dx.doi.org/10.1016/j.envsoft.2015.04.016
db/journals/envsoft/envsoft74.html#MaciejewskaJRK15

该文件比我发布的文件大。如何解决它?

StackOverflow将XML文件的编码弄乱了,因此我无法真正测试这一点,但请尝试以下方法:

import xml.etree.ElementTree as ET

parser = ET.XMLParser(encoding="iso-8859-1")
parser.parser.UseForeignDTD(True)
etree = ET.ElementTree()

tree = etree.parse('dblp_140.xml', parser=parser)

f = open('hi', 'w')
for country in tree.findall('article'):
    rank = country.find('year').text
    name = country.find('title')

    if(int(rank)>2009):
        f.write(name.text)
        f.write(':')
        auth = country.findall('author')
        for a in auth:
            print a.text.encode('utf-8')
            f.write(a.text.encode('utf-8'))  
            f.write(',')
        f.write('\n')

StackOverflow搞乱了XML文件的编码,因此我无法真正测试它,但请尝试以下方法:

import xml.etree.ElementTree as ET

parser = ET.XMLParser(encoding="iso-8859-1")
parser.parser.UseForeignDTD(True)
etree = ET.ElementTree()

tree = etree.parse('dblp_140.xml', parser=parser)

f = open('hi', 'w')
for country in tree.findall('article'):
    rank = country.find('year').text
    name = country.find('title')

    if(int(rank)>2009):
        f.write(name.text)
        f.write(':')
        auth = country.findall('author')
        for a in auth:
            print a.text.encode('utf-8')
            f.write(a.text.encode('utf-8'))  
            f.write(',')
        f.write('\n')

f.write(a.text.encode('utf8'))
更改为
f.write(a.text)
;它已经是UTF-8了。您使用的是哪个python版本?我正在使用python 2.7。任何其他错误都会出现。例如:u'\03b1',u'\xe8',u'\xe9',等等。请帮助。请显示完整的堆栈跟踪,并说明错误发生在哪一行。将
f.write(a.text.encode('utf8')
更改为
f.write(a.text)
;它已经是UTF-8。您使用的是哪个python版本?我使用的是python 2.7。还有其他错误。例如:u'\03b1',u'\xe8',u'\xe9',等等。请帮助。请显示完整的堆栈跟踪,并说明错误发生在哪一行。