This is head sample of my file:
1.1.1.0 - 1.1.1.255
2.2.2.0 - 2.2.2.255
3.3.3.0 - 3.3.3.100
This is my Python regex code:
regex = r'(?P<start>\w+\.\w+\.\w+\.\w+) \- (?P<end>\w+\.\w+\.\w+\.\w+)'
with open('sorted_french_ips.txt', 'r') as file:
with open('sorted_french_edited_ips.txt', 'a') as sorted_french_edited_ips:
for line in file:
find = re.match(regex, line)
start = find.group('start')
end = find.group('end')
cidr = netaddr.iprange_to_cidrs(start, end)
cidr = str(cidr)
cidr = cidr.replace(', IPNetwork', '\n')
cidr = cidr.replace('[IPNetwork(\'', '')
cidr = cidr.replace('\')]', '')
cidr = cidr.replace('(\'', '')
cidr = cidr.replace('\')', '')
sorted_french_edited_ips.write(f'{cidr}\n')
When I run this code in Windows, I get this error:
Traceback (most recent call last):
File "C:\Users\Saeed\Desktop\python-projects\win.py", line 131, in <module>
write()
File "C:\Users\Saeed\Desktop\python-projects\win.py", line 81, in write
start = find.group('start')
AttributeError: 'NoneType' object has no attribute 'group'
But if I run the same code in Linux, it works fine.
Why I get error in Windows? Are Window's regex and Linux different? Should it be changed?
dos2unix file.py.