Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.x多行正则表达式的问题?_Python_Regex_Xml - Fatal编程技术网

Python3.x多行正则表达式的问题?

Python3.x多行正则表达式的问题?,python,regex,xml,Python,Regex,Xml,我对python非常陌生,几周前就开始使用它编写代码。从现在起,我能够解决研究和阅读中的任何问题。但这个问题让我头疼了好几个小时,我无法找到正确的解决方案 我在硬盘上的read_xml.py文件所在的文件夹中创建了一个示例xml() read_xml.py(之前)的内容 再也找不到/匹配任何东西了…简言之:不要。如果您正在学习Python(或任何其他语言),通常会考虑使用正则表达式分析XML节点。相反,使用解析器(这就是它们的用途)。 对于您的特定示例,这可能归结为: from lxml im

我对python非常陌生,几周前就开始使用它编写代码。从现在起,我能够解决研究和阅读中的任何问题。但这个问题让我头疼了好几个小时,我无法找到正确的解决方案

我在硬盘上的read_xml.py文件所在的文件夹中创建了一个示例xml()

read_xml.py
(之前)的内容


再也找不到/匹配任何东西了…

简言之:不要。如果您正在学习
Python
(或任何其他语言),通常会考虑使用正则表达式分析
XML
节点。相反,使用解析器(这就是它们的用途)。

对于您的特定示例,这可能归结为:

from lxml import etree
tree = etree.parse('test.xml')
root = tree.getroot()

for title in root.xpath("//item/title"):
    print(title.text)
和产量

It's Bugtober, with Adobe Flash crashes, numerous CVE vulnerability patches for Wi-Fi and routers, and an Intel SPI vulnerability patch for most Xeon D Supermicro SuperServers
Supermicro Xeon D SuperServer BIOS 1.2c / IPMI 3.58 released
Windows 10 Fall Creators Update introduces GPU monitoring features built right into Task Manager
VMUG Advantage EVALExperience includes latest VMware vRealize Log Insight 4.5 syslog server appliance for easy vSphere, vSAN, IoT, and networking gear log file analysis
Road-warrior productivity boosted by ASUS ZenScreen MB16AC secondary travel display that connects to Mac or PC with just one USB-C or USB 3.0 cable

你看,这使得代码更清晰,更易于理解。您可能需要先通过
pip install lxml
安装


注意:您的XML文件中有一个错误,我需要对此进行更正才能正常工作(已打开
链接
标记,但从未关闭)。

感谢您的快速重播,与内置的XML.etree.ElementTree相比,lxml有什么优势?它具有更好的错误报告功能,并支持
HTML
from lxml import etree
tree = etree.parse('test.xml')
root = tree.getroot()

for title in root.xpath("//item/title"):
    print(title.text)
It's Bugtober, with Adobe Flash crashes, numerous CVE vulnerability patches for Wi-Fi and routers, and an Intel SPI vulnerability patch for most Xeon D Supermicro SuperServers
Supermicro Xeon D SuperServer BIOS 1.2c / IPMI 3.58 released
Windows 10 Fall Creators Update introduces GPU monitoring features built right into Task Manager
VMUG Advantage EVALExperience includes latest VMware vRealize Log Insight 4.5 syslog server appliance for easy vSphere, vSAN, IoT, and networking gear log file analysis
Road-warrior productivity boosted by ASUS ZenScreen MB16AC secondary travel display that connects to Mac or PC with just one USB-C or USB 3.0 cable