用UTF8编码和使用ElemT的bytestring解析XML文件

2024-10-04 09:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下完整的XML文件(实际文件可下载here):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MedlineCitationSet PUBLIC "-//NLM//DTD Medline Citation, 1st January, 2014//EN"
                                    "http://www.nlm.nih.gov/databases/dtd/nlmmedlinecitationset_140101.dtd">
<MedlineCitationSet>
<MedlineCitation Owner="NLM" Status="In-Data-Review">
<PMID Version="1">24560200</PMID>
<Article PubModel="Print-Electronic">
<Journal>
<ISSN IssnType="Print">1166-7087</ISSN>
<JournalIssue CitedMedium="Internet">
<Volume>24</Volume>
<Issue>3</Issue>
<PubDate>
<Year>2014</Year>
<Month>Mar</Month>
</PubDate>
</JournalIssue>
<Title>Progrès en urologie : journal de l'Association française d'urologie et de la Société française d'urologie</Title>
<ISOAbbreviation>Prog. Urol.</ISOAbbreviation>
</Journal>
<ArticleTitle>[Multiparametric 3T MRI in the routine staging of prostate cancer].</ArticleTitle>
<Pagination>
<MedlinePgn>145-53</MedlinePgn>
</Pagination>
<Abstract>
<AbstractText Label="RESULTS" NlmCategory="RESULTS">Five hundred and ninety-two octants were considered with 124 significant tumors (volume≥0.1cm(3)). The general ability of tumor detection had a sensitivity, specificity, PPV and NPV respectively to 72.3%, 87.4%, 83.2% and 78.5%. The estimate of the CC and ECE had a high negative predictive power with specificities and VPN respectively to 96.4% and 95.4% for CC, and 97.5 and 97.7% for ECE.</AbstractText>
<CopyrightInformation>Copyright © 2013 Elsevier Masson SAS. All rights reserved.</CopyrightInformation>
</Abstract>
</Article>
</MedlineCitation>
</MedlineCitationSet>

我要做的只是解析数据并打印PMID和title。 这是我的代码:

^{pr2}$

但是它给了我以下错误消息:

24560200 Traceback (most recent call last):
  File "./parse_xml.py", line 41, in <module>
    parse_xml(fvar)
  File "./parse_xml.py", line 29, in parse_xml
    print pmid, title
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 5: ordinal not in range(128)

正确的解析和打印方法是什么?在


Tags: and文件ofinparsearticlenlmxml