简体   繁体   English

使用 Python 从文本文件中提取 XML 数据

[英]Extract XML Data from a text file using Python

I have a text files in a one directory like "Rest.txt", "Test.txt".我在一个目录中有一个文本文件,如“Rest.txt”、“Test.txt”。 Sample string in a txt file as shown below. txt 文件中的示例字符串,如下所示。

Input : <Hello> <Message><stdout>"weblogic.servelt.Default(self-tuning)"] <12-18-2020> <?xml version="1.0" encoding="UTF-8"?><breakfast_menu><food><name>Belgian Waffles</name><price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description><calories>650</calories></food><food><name>Strawberry Belgian Waffles</name> <price>$7.95</price><hello><sjiadasjhds>954jkldfksfkfjsdfklsdjf输入<Hello> <Message><stdout>"weblogic.servelt.Default(self-tuning)"] <12-18-2020> <?xml version="1.0" encoding="UTF-8"?><breakfast_menu><food><name>Belgian Waffles</name><price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description><calories>650</calories></food><food><name>Strawberry Belgian Waffles</name> <price>$7.95</price><hello><sjiadasjhds>954jkldfksfkfjsdfklsdjf

In between the above given string there has XML tags.在上面给定的字符串之间有 XML 标签。 I need to extract all XML tags and save it in another directory using python.我需要使用 python 提取所有 XML 标签并将其保存在另一个目录中。 Like this I need to extract all XML data in all text files and save it.像这样我需要提取所有文本文件中的所有 XML 数据并保存它。

Output :输出

<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price> 
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description><calories>650</calories>
</food>
</breakfast_menu>```

You can extract the tag using this code您可以使用此代码提取标签

from bs4 import BeautifulSoup
a = open("test.txt","r").read()
soup = BeautifulSoup(a, 'xml')
result = soup.find("breakfast_menu")

the result is what you need now you can write it in a file you want结果就是你现在需要的你可以把它写在你想要的文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM