Answer the question
In order to leave comments, you need to log in
How to extract IPv4 addresses from a file and save them to another using Python?
I have a database dump in sql format with over 10000 records. Question. I'm trying to extract IPv4 addresses from there that are stored there using a Python parser script. The script works, but not correctly. It turned out to find 70 IP addresses as much as possible, and even then there are some invalid ones. Although there are more than 10,000 entries in the dump. How to competently organize a script for extracting IP addresses from a dump using Python?
Ps I tried to google, nothing helped.
Answer the question
In order to leave comments, you need to log in
Most likely the problem is in the search itself.
Instead of just regexp IP search over the entire string (re.search, re.findall) you are trying to split it into words:
wordList = item.split()
But this also splits by spaces. And if there are no spaces, then nothing will be found, especially since you only replace [',', ';'] with spaces. If there is a line in the dump, for example
, then it will not find them.
It's easier to take the whole line and search through re.findall (leave the template itself the same).
insert into .... values ('1.1.1.1','5.5.5.5')
Of course, you need to get the regexp, here are the recipes through the roof - https://www.oreilly.com/library/view/regular-expre...
Run sql into the sqlite3 database, then use the script to pull the ip-addresses from the desired column. Or throw off an example sql file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question