我有两个文件,我想相互检查,并取出它们所包含的行。
我试着用regex做这件事,但我一直收到这个错误,我认为这是因为我正在访问一个文件,而不仅仅是直接呈现一个字符串
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.2544.0_x64__qbz5n2kfra8p0libre.py", line 248, in finditer
return _compile(pattern, flags).finditer(string)
TypeError: expected string or bytes-like object
这就是我用于正则表达式搜索的内容
regex = r"d(.*(10.0.0.0).*)"
with open('test1.txt', 'r') as file1:
test = file1.read().splitlines()
matches = re.finditer(regex, test)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
我也尝试过,但仍然会出现错误
with open("test1.txt") as file1, open("test2") as file2:
st = set(map(str.rstrip,file1))
for line in file2:
spl = line.split(None, 1)[0]
if spl in st:
print(line.rstrip())
错误为
IndexError: list index out of range
我正在尝试将IP列表与路由器的输出进行匹配,因此test2文件看起来像这个
10.0.0.0/8
11.0.0.0/8
12.0.0.0/8
13.0.0.0/8
路由器输出看起来像
1 X test 10.0.0.0/8 nov/19/2021 13:03:08
2 X test 11.0.0.0/8 nov/19/2021 13:03:08
3 X test 12.0.0.0/8 nov/19/2021 13:03:08
4 X test 13.0.0.0/8 nov/19/2021 13:03:08
我希望路由器的整条线路只与IP匹配,而不是必须将整个预期输出
希望这已经足够了,对这一切来说仍然很新鲜,欢呼
完整答案为
with open("router_output.txt") as file1, open("list_of_ips.txt") as file2:
ips_to_keep = file2.read().splitlines()
router_lines = file1.read().splitlines()
ips_to_keep = [" " + ip + " " for ip in ips_to_keep]
for line in router_lines:
if any(ip in line for ip in ips_to_keep):
print(line)
假设您的文件有空格而没有制表符:(
如果您有一个包含实际IP的文件,并且不需要正则表达式,那么您可以执行类似的操作
my_str = """
1 X test 10.0.0.0/8 nov/19/2021 13:03:08
2 X test 11.0.0.0/9 nov/19/2021 13:03:08
3 X test 12.0.0.0/12 nov/19/2021 13:03:08
4 X test 13.0.0.0/2 nov/19/2021 13:03:08
5 X test 555.0.0.0/2 nov/19/2021 13:03:08 #expecting this to not be printed
"""
keep_ips = [' 10.0.0.0/8 ', ' 11.0.0.0/9 ', ' 12.0.0.0/12 ', ' 13.0.0.0/2 ']
for line in my_str.split('n'):
if any(ip in line for ip in keep_ips):
print(line)
我在keep_ips
中添加了空格填充,因为否则你可以在类似113.0.0.0/25
的东西上匹配,因为它包含子字符串13.0.0.0/2
您可以重构此代码以读取路由器的行,然后读取IP的行,在IP的任一端添加空格,然后使用any
使用此逻辑
我希望这对你现在有效