python - 如何读取xml文件的一些内容并将其写入文本文件?

标签 python xml text

我有一个以下xml文件,我想读取<seg>中的内容并使用 Python 将它们保存到纯文本文件中。我使用了 DOM 模块。

<?xml version="1.0"?>
<mteval>
  <tstset setid="default" srclang="any" trglang="TRGLANG" sysid="SYSID">
    <doc docid="ntpmt-dev-2000/even1k.cn.seg.txt">
      <seg id="1">therefore , can be obtained having excellent properties ( good stability and solubility of the balance of the crystal as a pharmaceutical compound is not possible to predict .</seg>
      <seg id="3">compound ( I ) are preferably crystalline , in particular , has good stability and solubility equilibrium and suitable for industrial prepared type A crystal is preferred .</seg>
      <seg id="4">method B included in the catalyst such as DMF , and the like in the presence of a compound of formula ( II ) with thionyl chloride or oxalyl chloride to give an acyl chloride , in the presence of a base of the acid chloride with alcohol ( IV ) ( O ) by reaction of esterification .</seg>
    </doc>
  </tstset>
</mteval>

from xml.dom.minidom import parse
import xml.dom.minidom

dom = xml.dom.minidom.parse(r"path_to_xml file")
file = dom.documentElement
seg = dom.getElementsByTagName("seg")
for item in seg:
    sent = item.firstChild.data
    print(sent,sep='')

file = open(r'file.txt','w')
file.write(sent)
file.close()

运行上面的代码时,它成功打印了屏幕上的所有行,但file.txt只有最后一行<seg> (seg id=4),实际上我想将所有句子保存到文件中。我的代码有问题吗?

最佳答案

您只需调用 file.write(sent) 一次,在循环之前打开文件,然后将以下行添加到此代码中:

file = open(r'file.txt','w')

for item in seg:
    sent = item.firstChild.data
    print(sent,sep='')
    file.write(sent) // <---- this line

file.close()

关于python - 如何读取xml文件的一些内容并将其写入文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501150/

相关文章:

c++ - 需要 C++ 函数的帮助来解析/显示 XML 序列化

java - 在 Java Applet 中读取 XML

python - 如何读取一个非常大的文本文件的最后 MB

python - 让文本显示在子图图像前面

python - Pandas.DataFrame.resample 出现意外结果

python - 我可以使用 PIL 获取两个字符的字距调整值吗?

当节点内部文本为html时,Java解析xml文件

python - 仅重新索引 MultiIndex 数据帧的级别,reindex() 损坏了吗?

python - 为什么 Django 1.0.x 无法从 PyPI 安装?

python - 在Python中查找文本中是否存在单词的逻辑