我正在尝试通过'difflib'库进行文本比较。
我想知道如何仅获得第一个字符串序列相对于第二个字符串序列的术语。
例如:
import difflib
one = "If rents are received later than five (5)"
two = "If rents are received later than eight (8)"
n_one = one.replace(" ","\n")
n_two = two.replace(" ","\n")
diff = difflib.ndiff(n_one.splitlines(1),n_two.splitlines(1))
print ''.join(diff)"
# ...
# - five
# - (5) + eight
# + 8
我想知道如何获得两个字符串:
->第一个字符串的差异:
['five','(5)']
->第二个字符串的差异:
['eight','(8)']
答案 0 :(得分:1)
if (driver.FindElements(By.XPath("//lina-device-information/div[1]/div[2]/lina-list-wrapper[3]/div/div/lina-insurance-information")).Count > 0)
{
// Do stuff
// Element found
}
else{
// Element not found
}
答案 1 :(得分:0)
单线方式,不使用difflib:
>>> first, second = zip(*[(a, b) for a, b in zip(one.split(" "), two.split(" ")) if a != b])
>>> first
('five', '(5)')
>>> second
('eight', '(8)')
这当然有效,因为我们在这里处理单个字符串输入,并且字符串在完全相同的位置处不同。如果第二个字符串以"eight(8)"
结尾,则会在差异中错过'(5)'
。