【问题标题】:When replace xml contents containing special characters during parsing在解析过程中替换包含特殊字符的xml内容时
【发布时间】:2019-09-29 15:35:07
【问题描述】:

我正在解析一个 3gb xml 文件,该文件在 python 中具有复杂的 xml.sax 结构。 当 3gb xml 文件包含特殊字符如“

我想知道如何同时用“

我试图通过正则表达式来捕捉它的内容,但这对我来说并不容易。

<DC.Title><main>Characteristics of the interaction mechanism between tannic <acid> and sodium caseinate using multispectroscopic and thermodynamics methods</main>
</DC.Title>

从中,我想提取以下内容。

Characteristics of the interaction mechanism between tannic <acid> and sodium caseinate using multispectroscopic and thermodynamics methods

但由于'

我尝试使用 ContentHandler 类中的方法。

使用正则表达式,替换特殊字符很难,python中的xml.sax无法处理这个问题。

我想我应该交换每个特殊字符,例如“

============================================== 添加在下面(实际xml)

<NDSL_Articles>
<Article><DC.Identifier><controlNumber>73113660</controlNumber>
<controlNumber.source.BL>RN602387147</controlNumber.source.BL><controlNumber.source>JAKO201857968658354</controlNumber.source>
<journal scheme="URL">http://society.kisti.re.kr/journal/kj_view.jsp?kj=HJTODO&amp;soc=etri&amp;ndsl=y</journal>
<article scheme="URL">http://society.kisti.re.kr/journal/view.jsp?soc=etri&amp;kj=HJTODO&amp;py=2018&amp;vol=40&amp;iss=2&amp;sp=283&amp;ndsl=y</article>
<article.source scheme="KOI">KISTI1.1003/JNL.JAKO201857968658354</article.source>
<article.source scheme="URL">http://koix.kisti.re.kr/KISTI1.1003/JNL.JAKO201857968658354</article.source>
<article scheme="DOI">http://dx.doi.org/10.4218/etrij.15.0114.0065</article>
<article.source scheme="ACMS_CN2">etri/HJTODO_2018_v40n2_283</article.source>
<paper scheme="ISSN">1225-6463</paper>
<publicationID.source>HJTODO</publicationID.source>
</DC.Identifier>
<DC.Relation><isPartOf>
<title>ETRI Journal</title>
<volume>v.40 no.2</volume>
<sourcePage>283-283</sourcePage>
<startPage>283</startPage>
<lastPage>283</lastPage>
<type>Journal</type>
</isPartOf></DC.Relation>
<DC.Description><reference.count>0</reference.count></DC.Description>
<DC.Format><Pages>1</Pages></DC.Format>
<DC.Language><text scheme="USMARC">eng</text></DC.Language>
<DC.Creator><personal><main>Hong, Kang Woon</main><affiliation>Department of Information and Communications Engineering, KAIST, Broadcasting &amp; Telecommunications Media Research Laboratory, ETRI</affiliation><email>kangwoon@kaist.ac.kr, gwhong@etri.re.kr</email></personal><personal><main>Ryu, Won</main><affiliation>Broadcasting &amp; Telecommunications Media Research Laboratory, ETRI</affiliation></personal></DC.Creator>
<DC.Title><main>Corrigendum</main>
</DC.Title>
<DC.Publisher><main>Electronics and Telecommunications Research Institute</main><alternative>한국전자통신연구원</alternative></DC.Publisher>
<DC.Date><created scheme="ISO 8601">2018-04-01</created></DC.Date>
<DC.Type>Article</DC.Type>
<NDSL.Usage scheme="freetext">eletronic</NDSL.Usage>
<NDSL.Cataloging>  <instituion scheme="Internal">BL</instituion>  <source.version>KISTI XML기반의 학술정보 및 협회기술정보 가공 지침서 v.1.0</source.version>  <date scheme="ISO 8601">2015-09-25T13:48:09</date>  <name>BL</name>  <instituion.lastUpdate scheme="Internal">NDSL 센터</instituion.lastUpdate>  <date.lastUpdate scheme="ISO 8601">2018-07-12T11:17:45</date.lastUpdate>  <name.lastUpdate>김순영</name.lastUpdate>  </NDSL.Cataloging>
</Article>

<DC.DOI>
<doi>http://dx.doi.org/10.4218/etrij.15.0114.0065</doi>
</DC.DOI>
</NDSL_Articles>

【问题讨论】:

  • 请发布您的相关实际代码并确定报告的具体错误。
  • 问题是没有&lt;/acid&gt;结束标签,导致输入格式不正确。
  • 我的意思是 '' 不是标签只是特殊字符!

标签: python xml


【解决方案1】:

根据您提供的少量信息,假设您的所有数据都具有相似的格式。我会使用BeautifulSoup 来提取数据。

from bs4 import BeautifulSoup
a = BeautifulSoup("<DC.Title><main>Characteristics of the interaction mechanism between tannic <acid> and sodium caseinate using multispectroscopic and thermodynamics methods</main></DC.Title>", "html.parser")
print(a.main)

例如上面的代码打印出&lt;main&gt;的内容:

&lt;main&gt;Characteristics of the interaction mechanism between tannic &lt;acid&gt; and sodium caseinate using multispectroscopic and thermodynamics methods&lt;/acid&gt;&lt;/main&gt;

&lt;acid&gt; 之类的标签会阻止我们使用 a.text,我没有足够的信息来帮助您解决这个问题,所以您必须自己处理。

【讨论】:

  • 我的文件是3gb的文件,这意味着我在使用beautifulsoup时出现内存错误。这就是我尝试使用萨克斯的原因。
  • 可能分成不同的文件?我建议您至少发布文件的 3 段,以便我们至少知道如何处理
  • 我添加了一些xml。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多