【发布时间】:2016-12-24 18:24:57
【问题描述】:
我需要在一些 XML 文件(它们都具有相同的名称,pom.xml)中搜索以下文本序列(也在子文件夹中),所以如果有人写一些文本甚至是空白,我必须收到警报:
<!--
| Startsection
|-->
<!--
| Endsection
|-->
我正在运行以下 Python 脚本,但仍然不完全匹配,即使它部分是里面的文本,我也会收到警报:
import re
import os
from os.path import join
comment=re.compile(r"<!--\s+| Startsection\s+|-->\s+<!--\s+| Endsection\s+|-->")
tag="<module>"
for root, dirs, files in os.walk("."):
if "pom.xml" in files:
p=join(root, "pom.xml")
print("Checking",p)
with open(p) as f:
s=f.read()
if tag in s and comment.search(s):
print("Matched",p)
更新 #3
我希望打印出标签<module>的内容,如果它存在于|--> <!--之间
进入搜索:
<!--
| Startsection
|-->
<!--
| Endsection
|-->
例如在 Matched 之后打印,以及文件的名称,在下面的情况下也打印“example.test1”:
<!--
| Startsection
|-->
<module>example.test1</module>
<!--
| Endsection
|-->
更新 #4
应该使用以下内容:
import re
import os
from os.path import join
comment=re.compile(r"<!--\s+\| Startsection\s+\|-->\s+<!--\s+\| Endsection\s+\|-->", re.MULTILINE)
tag="<module>"
for root, dirs, files in os.walk("/home/temp/test_folder/"):
for skipped in ("test1", "test2", ".repotest"):
if skipped in dirs: dirs.remove(skipped)
if "pom.xml" in files:
p=join(root, "pom.xml")
print("Checking",p)
with open(p) as f:
s=f.read()
if tag in s and comment.search(s):
print("The following files are corrupted ",p)
更新 #5
import re
import os
import xml.etree.ElementTree as etree
from bs4 import BeautifulSoup
from bs4 import Comment
from os.path import join
comment=re.compile(r"<!--\s+\| Startsection\s+\|-->\s+<!--\s+\| Endsection\s+\|-->", re.MULTILINE)
tag="<module>"
for root, dirs, files in os.walk("myfolder"):
for skipped in ("model", "doc"):
if skipped in dirs: dirs.remove(skipped)
if "pom.xml" in files:
p=join(root, "pom.xml")
print("Checking",p)
with open(p) as f:
s=f.read()
if tag in s and comment.search(s):
print("ERROR: The following file are corrupted",p)
bs = BeautifulSoup(open(p), "html.parser")
# Extract all comments
comments=soup.find_all(string=lambda text:isinstance(text,Comment))
for c in comments:
# Check if it's the start of the code
if "Start of user code" in c:
modules = [m for m in c.findNextSiblings(name='module')]
for mod in modules:
print(mod.text)
【问题讨论】:
-
请不要使用正则表达式解析 XML。这是一个糟糕的想法,它让经验丰富的程序员哭泣。试试BeautifulSoup 或其底层库lxml
-
我正在考虑将确切的序列存储在外部文件中。我该如何实施?你能帮我解决这个问题吗?谢谢!
-
@AdamSmith, ...这里的困难是他们想要找到评论,所以它实际上并没有出现在 DOM 树中。
-
顺便说一句,当创建一个与旧问题密切相关的新问题时(在这种情况下,stackoverflow.com/questions/38958403/… 的 Python 而非 shell 实例)被认为是包含链接的好形式,并且明确描述它们的区别。
-
@CharlesDuffy cmets 可以使用
comment()函数在 XPath 和 XSLT 中进行解析。