i have a txt with some IPs,domains and urls but the list is not organized. I want to divide the IP, domain and url like that:
List before:
1.1.1.1
domain.com
2.2.2.2
3.3.3.3
domain2.com
List after:
IP
1.1.1.1
2.2.2.2
3.3.3.3
DOMAIN
domain.com
domain2.com
URL
How can i do it? Thanks!
CodePudding user response:
I think thats what you need, you must install validators
with the following command pip install validators
. Hope this helps
Code:
import validators
import re
my_dict = dict()
with open('config.txt') as f:
for line in f:
if bool(re.match(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", line)):
my_dict.setdefault('IP', []).append(line)
elif validators.url(line):
my_dict.setdefault('URL', []).append(line)
elif validators.domain(line):
my_dict.setdefault('DOMAIN', []).append(line)
for key in my_dict.keys():
print(key, '\n')
for value in my_dict[key]:
print(value , '\n')
Output:
IP
1.1.1.1
2.2.2.2
3.3.3.3
DOMAIN
domain.com
domain2.com
URL
https://url.com/test
you should also replace config.txt
with your text file