import re
Hosts = ["gvff", "etc"]
def list():
with open('C:/Users/user/Desktop/scripts/devices.txt', 'r') as file:
for row in file:
device = row.rsplit('\n')
devices = re.search("^(.*?)\,(.*?)\,(.*?)\,", device[0])
if devices != None:
device = devices.group(1)
hardwaretype = devices.group(2)
level = devices.group(3)
for host in Hosts:
if device.startswith(host):
if level == 'Production' or level == 'Production-Secondary':
print(f'| {device} | {hardwaretype} | {level} |')
list()
Hey I'm not getting anywhere at the moment. I want to compare the hosts list with the devices.txt. It currently prints me everything it has matched, but I need the items that could not be matched. How could I do that here?
Current Output: | blabla.bla.bla | physical | Production | this host is in the device.txt and in the Hosts list, so its the matching output
Excepted Output: looks like above, but only the hosts which are in devices.txt but not in Hosts List.
Many greetings
CodePudding user response:
Just add the not
keyword before device.startswith(host)
, it will make the condition true
if the device
not starts with 'host' otherwise false
.
change
if device.startswith(host):
...
to
if not device.startswith(host):
...
CodePudding user response:
example host in device.txt: blabla.blabla.blabla, physical, Production