简体   繁体   English

如何从第二个字符串中的字符串中删除行?

[英]How to delete lines from a string that are in a second string?

I have two strings:我有两个字符串:

String A:字符串 A:

machine1 volume1 Mon May 24 00:00:10 2021 
machine2 volume1 Mon May 24 00:00:03 2021 
machine2 volume2 Mon May 24 00:00:03 2021

String B:字符串 B:

machine1 volume2 Mon May 23 00:00:10 2021 
machine2 volume1 Mon May 23 00:00:03 2021 
machine2 volume2 Mon May 24 00:00:03 2021

And I would like to "remove" from String A, all the lines that are in String B, so the result could be something like:我想从字符串 A 中“删除”字符串 B 中的所有行,因此结果可能类似于:

New String A:新字符串 A:

machine1 volume1 Mon May 24 00:00:10 2021 
machine2 volume1 Mon May 24 00:00:03 2021 

I tried with this:我试过这个:

avoid = set(s2.splitlines())
result = "\n".join(x for x in s1.splitlines() if x not in avoid)
print (result)

But the result still contains some lines from the second string...但结果仍然包含第二个字符串中的一些行......

You may have some padding spaces at the end of some lines, and a different amount between the s1 and the s2 , so you may use rstrip() to fix that您可能在某些行的末尾有一些填充空间,并且s1s2之间的数量不同,因此您可以使用rstrip()来修复它

That will keep the leading spaces in the result这将保留结果中的前导空格

avoid = {x.rstrip() for x in s2.splitlines()}
result = "\n".join(x for x in s1.splitlines() if x.rstrip() not in avoid)

That will strip leading spaces in the result这将去除结果中的前导空格

avoid = {x.rstrip() for x in s2.splitlines()}
result = "\n".join(x.rstrip() for x in s1.splitlines() if x.rstrip() not in avoid)

Try this:尝试这个:

str1="machine1 volume1 Mon May 24 00:00:10 2021\nmachine2 volume1 Mon May 24 00:00:03 2021\nmachine2 volume2 Mon May 24 00:00:03 2021"
str2="machine1 volume2 Mon May 23 00:00:10 2021\nmachine2 volume1 Mon May 23 00:00:03 2021\nmachine2 volume2 Mon May 24 00:00:03 2021"
list1=str1.split("\n") #=== Convert to list
list2=str2.split("\n")
newlist=[x for x in list1 if x not in list2] #== list comprehension, if x in list2, add it to newlist
print(str(newlist)) 

I ran your code on Python 3.8.5, and got the output:我在 Python 3.8.5 上运行了您的代码,得到了 output:

machine1 volume1 Mon May 24 00:00:10 2021 
machine2 volume1 Mon May 24 00:00:03 2021

which doesn't contain any strings from String B.其中不包含字符串 B 中的任何字符串。

Perhaps look into how your strings are formatted, to see if there's any spaces or odd new line characters at the end of the lines throwing off the string comparison?也许看看你的字符串是如何格式化的,看看在行尾是否有空格或奇怪的换行符会导致字符串比较失败?

One liner,一个班轮,

sA = f'machine1 volume1 Mon May 24 00:00:10 2021\n\
machine2 volume1 Mon May 24 00:00:03 2021\n\
machine2 volume2 Mon May 24 00:00:03 2021'

sB = f'machine1 volume2 Mon May 23 00:00:10 2021 \n\
machine2 volume1 Mon May 23 00:00:03 2021\n\
machine2 volume2 Mon May 24 00:00:03 2021'
print('\n'.join(x for x in sA.splitlines() if x not in sB.splitlines()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM