我尝试了各种各样的逻辑和方法,甚至在google上搜索了很多,但是对于我所提出的问题,却没有找到任何令人满意的答案。我编写了一个如下所示的程序来突出显示我所面临的一些问题的特定xml代码。抱歉让这篇文章有点长。我只想清楚地解释我的问题。在
编辑:为了运行下面给定的程序,您需要两个xml文件:sample1和sample2。保存此文件,然后在下面的代码中编辑要将文件保存在C:/Users/editThisLocation/Desktop/sample1.xml
中的位置
from lxml import etree
from collections import defaultdict
from collections import OrderedDict
from distutils.filelist import findall
from lxml._elementpath import findtext
from Tkinter import *
import Tkinter as tk
import ttk
root = Tk()
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start, end,
regexp=True):
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
def Remove_pattern(self, pattern, tag, start="1.0", end="end",
regexp=True):
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_remove(tag, start, end)
recovering_parser = etree.XMLParser(recover=True)
sample1File = open('C:/Users/editThisLocation/Desktop/sample1.xml', 'r')
contents_sample1 = sample1File.read()
sample2File = open('C:/Users/editThisLocation/Desktop/sample2.xml', 'r')
contents_sample2 = sample2File.read()
frame1 = Frame(width=768, height=25, bg="#000000", colormap="new")
frame1.pack()
Label(frame1, text="sample 1 below - scroll to see more").pack()
textbox = CustomText(root)
textbox.insert(END,contents_sample1)
textbox.pack(expand=1, fill=BOTH)
frame2 = Frame(width=768, height=25, bg="#000000", colormap="new")
frame2.pack()
Label(frame2, text="sample 2 below - scroll to see more").pack()
textbox1 = CustomText(root)
textbox1.insert(END,contents_sample2)
textbox1.pack(expand=1, fill=BOTH)
sample1 = etree.parse("C:/Users/editThisLocation/Desktop/sample1.xml", parser=recovering_parser).getroot()
sample2 = etree.parse("C:/Users/editThisLocation/Desktop/sample2.xml", parser=recovering_parser).getroot()
ToStringsample1 = etree.tostring(sample1)
sample1String = etree.fromstring(ToStringsample1, parser=recovering_parser)
ToStringsample2 = etree.tostring(sample2)
sample2String = etree.fromstring(ToStringsample2, parser=recovering_parser)
timesample1 = sample1String.findall('{http://www.example.org/eHorizon}time')
timesample2 = sample2String.findall('{http://www.example.org/eHorizon}time')
for i,j in zip(timesample1,timesample2):
for k,l in zip(i.findall("{http://www.example.org/eHorizon}feature"), j.findall("{http://www.example.org/eHorizon}feature")):
if [k.attrib.get('color'), k.attrib.get('type')] != [l.attrib.get('color'), l.attrib.get('type')]:
faultyLine = [k.attrib.get('color'), k.attrib.get('type'), k.text]
def high(event):
textbox.tag_configure("yellow", background="yellow")
limit_1 = '<p1:time nTimestamp="{0}">'.format(5) #limit my search between timestamp 5 and timestamp 6
limit_2 = '<p1:time nTimestamp="{0}">'.format((5+1)) # timestamp 6
highlightString = '<p1:feature color="{0}" type="{1}">{2}</p1:feature>'.format(faultyLine[0],faultyLine[1],faultyLine[2]) #string to be highlighted
textbox.highlight_pattern(limit_1, "yellow", start=textbox.search(limit_1, '1.0', stopindex=END), end=textbox.search(limit_2, '1.0', stopindex=END))
textbox.highlight_pattern(highlightString, "yellow", start=textbox.search(limit_1, '1.0', stopindex=END), end=textbox.search(limit_2, '1.0', stopindex=END))
button = 'press here to highlight error line'
c = ttk.Label(root, text=button)
c.bind("<Button-1>",high)
c.pack()
root.mainloop()
我想要什么
如果您运行上述代码,它将显示以下输出:
正如您在图片中看到的,我只想突出显示标有绿色记号的代码。有些人可能会考虑限制起始和结束索引来突出这种模式。但是,如果您在我的程序中看到,我已经在使用开始和结束索引来将我的输出限制为nTimestamp="5"
,为此我使用了limit_1
和{
那么在这种类型的数据中,如何正确地突出显示单个nTimestamp
中的一个模式?在
编辑:这里我特别想突出显示nTimestamp="5"
中的第三项,因为这个项不存在于{
我正在使用BryanOakley的代码中的突出显示类here
编辑最近的
对于kobejohn在评论中提出的问题,目标文件永远不会是空的。目标文件总是有可能有额外的或缺少的元素。最后,我现在的目的是只强调不同或缺失的深层元素和它们所在的timestamps
。但是,高亮显示timestamps
是正确的,但是像上面解释的那样突出显示深层元素的问题仍然是一个问题。谢谢你的澄清。在
注意:
我知道一个方法,你可能会建议正确的工作是提取绿色勾选图案的索引,并简单地在它上面运行highlight标记,但这种方法是非常硬编码的,在大数据中,你必须处理大量的变化,它是完全无效的。我正在寻找另一个更好的选择。在
此解决方案的工作原理是根据您提供的描述执行}之间的简化差异。diff结果是第三个XML树,它合并了原始树。输出是对文件之间不匹配的行使用颜色编码高亮显示的diff。在
base.xml
和{我希望你能用这个或者用它来适应你的需要。在
复制粘贴脚本
输入:
请注意,我清理了xml,因为我的行为与原始xml不一致。最初的基本上是使用反斜杠而不是正斜杠,并且在开始标记上也有错误的结束斜杠。在
^{pr2}$base.xml
(与此脚本位于同一位置)test.xml
(与此脚本位于同一位置)相关问题 更多 >
编程相关推荐