【发布时间】:2017-08-22 00:56:23
【问题描述】:
我正在从事一个涉及解析、比较和验证两个长文本的项目——其中一些是数千行文本。这些文件确实有共同的行和模式,但总体上有所不同。我感兴趣的是两个文件中的独特行。以下场景就是一个很好的例子:
文件1 -
- This file is located in 3000.3422.63.34 description "the mother of all files"
- City address of file is "Melbourne"
- Country of file is Australia
文件2-
-This file is located in 3000.3422.62.89 description "the brother of all good files"
- City address of file is "Sydney"
- This file spent sometime in "Gold Coast"
- Country of file is Australia
任务是使用 file1 作为参考来验证 file2 - 使用模式检查。 我想屏蔽两个文件的共同模式(见下文)并进行比较。
- This is the first file located in 3000.3422.xxxx.xxxx description "xxxx"
- City address of file is "xxxx"
- Country of file is xxxx
使用这个逻辑。第二个文件有一个独特的行,我将其导出到报告函数:
- This file spent sometime in "Gold Coast"
我怎样才能轻松地[在两个文件上] 进行动态屏蔽 - 感谢您的帮助?
【问题讨论】:
-
有什么方法可以轻松地在正则表达式中进行屏蔽? @downshift
-
据我所知,这不是正则表达式的好用例。使用正则表达式(相对于另一种技术)轻松完成可能是不合理的。我的意思是,可能可以使用正则表达式来完成,但更直接的方法可能更容易和更有效。您有什么理由希望 regex 解决方案优于传统的逐行比较?也许考虑使用 python 的
set()运算符的传统解决方案:stackoverflow.com/questions/19049020/python-unique-lines -
我已经进行了逐行比较。但是输出是如此巨大,因为它标记出了所有差异,即使它们属于同一类别。如果我使用上述方法屏蔽它们,它将大大减少独特行的数量 - 而且我不必修改我以前的函数。
-
搜索前你知道文件中的常用文本吗?我的意思是,你会有例如
patterns = ["- This file is located in 3000.3422", "- City address of file is", "- Country of file is Australia"]?
标签: python-2.7