如何读取Python的特殊字符

时间:2017-04-24 02:04:38

标签: python xml beautifulsoup

我正在使用某些作者名称(í = í , ï = ï , ò = ò etc)中的外语特殊字符解析XML文件。我的代码遇到错误" ExpatError:未定义的实体:"当试图处理这些字符时。我在网上看过BeautifulSoup库,但不知道如何轻松地将其实现到我的代码中而不必使用lxml库重写(如果我的理解是正确的)。解决这个问题的最佳方法是什么?干杯!

要加载的XML数据

<pub>
    <ID>75</ID>
    <title>Use of Lexicon Density in Evaluating Word Recognizers</title>
    <year>2000</year>
    <booktitle>Multiple Classifier Systems</booktitle>
    <pages>310-319</pages>
    <authors>
        <author>Petr Slav&iacute;k</author>
        <author>Venu Govindaraju</author>
    </authors>
</pub>

Python代码

import sqlite3
con = sqlite3.connect("publications.db")
cur = con.cursor()

from xml.dom import minidom

xmldoc = minidom.parse("test.xml")

#loop through <pub> tags to find number of pubs to grab
root = xmldoc.getElementsByTagName("root")[0]
pubs = [a.firstChild.data for a in root.getElementsByTagName("pub")]
num_pubs = len(pubs)
count = 0

while(count < num_pubs):

    #get data from each <pub> tag
    temp_pub = root.getElementsByTagName("pub")[count]
    temp_ID = temp_pub.getElementsByTagName("ID")[0].firstChild.data
    temp_title = temp_pub.getElementsByTagName("title")[0].firstChild.data
    temp_year = temp_pub.getElementsByTagName("year")[0].firstChild.data
    temp_booktitle = temp_pub.getElementsByTagName("booktitle")[0].firstChild.data
    temp_pages = temp_pub.getElementsByTagName("pages")[0].firstChild.data
    temp_authors = temp_pub.getElementsByTagName("authors")[0]
    temp_author_array = [a.firstChild.data for a in temp_authors.getElementsByTagName("author")]
    num_authors = len(temp_author_array)
    count = count + 1


    #process results into sqlite
    pub_params = (temp_ID, temp_title)
    cur.execute("INSERT INTO publication (id, ptitle) VALUES (?, ?)", pub_params)
    journal_params = (temp_booktitle, temp_pages, temp_year)
    cur.execute("INSERT INTO journal (jtitle, pages, year) VALUES (?, ?, ?)", journal_params)
    x = 0
    while(x < num_authors):
        cur.execute("INSERT OR IGNORE INTO authors (name) VALUES (?)", (temp_author_array[x],))
        x = x + 1

    #display results
    print("\nEntry processed: ", count)
    print("------------------\nPublication ID: ", temp_ID)
    print("Publication Title: ", temp_title)
    print("Year: ", temp_year)
    print("Journal title: ", temp_booktitle)
    print("Pages: ", temp_pages)
    i = 0
    print("Authors: ")
    while(i < num_authors):
        print("-",temp_author_array[i])
        i = i + 1

con.commit()
con.close()    

print("\nNumber of entries processed: ", count)  

2 个答案:

答案 0 :(得分:1)

如果您使用的是python3.x,只需导入html即可解码首先提取的数据

html.unescape(S)

将字符串s中的所有命名和数字字符引用(例如&gt;,&gt;,&amp; x3e;)转换为相应的unicode字符。

>>import html
>>print(html.unescape("Petr Slav&iacute;k"))

Petr Slavík
  

似乎html-safe字符无法解析并由minidom作为Document对象返回,您必须读取文件并对其进行解码,然后将其作为字符串发送到模块,如下面的代码。

xml.dom.minidom.parseString(string [,parser])

返回表示字符串的Document。

file_text = html.unescape(open('text.xml', 'r').read())
xmldoc = minidom.parseString(file_text)

答案 1 :(得分:1)

.encode('UTF-8') #Add to your code at the end of the example

UTF-8支持大多数这些字符, 应该管用, 添加:

xmldoc = minidom.parse("test.xml")
NewXML = xmldoc.encode('utf-8', 'ignore')