Jul-14-2018, 05:50 PM
It will work if you change what @ichabod801 posted.
Here to show other way with
Taken
Now only line bye line get read to memory.
This can be useful if your file was bigger that this.
Here to show other way with
re.finditer()
Taken
read()
(all to string) and findall()
(all to list) out of picture.Now only line bye line get read to memory.
This can be useful if your file was bigger that this.
import re with open('test.txt') as f,open('ip.txt','w') as f_out: for line in f: ip = re.compile(r'\d+\.\d+\.\d+\.\d+') for match in ip.finditer(line): f_out.write(f'{match.group()}\n')
Output:192.168.1.108
192.168.1.105
192.168.1.102
192.168.1.101
192.168.1.100
192.168.1.10