Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match and extract if found
#8
(Sep-08-2022, 08:30 AM)DeaD_EyE Wrote: Use a set, if the order is not required.

from io import StringIO
from ipaddress import ip_address, IPv4Address


# fake file for test
ip1_content = StringIO(
    """192.168.0.1
192.168.0.2
192.168.0.3
2001:4800:7819:104:be76:4eff:fe04:5819
192.168.0.4
192.168.0.5
192.168.0.6"""
)


ip2_content = StringIO(
    """
192.168.0.799
192.168.0.900
192.168.0.3
2001:4800:7819:104:be76:4eff:fe04:5819
192.168.0.1000
192.168.0.5
192.168.0.83"""
)


def make_ipv4_set(file_like):
    results = set()

    for line in map(str.strip, file_like):
        try:
            addr = ip_address(line)
        except ValueError:
            continue

        if isinstance(addr, IPv4Address):
            results.add(addr)
            # or if you want str
            # results.add(line)

    return results


results = make_ipv4_set(ip1_content) - make_ipv4_set(ip2_content)
print(results)

Works like charm but is it possible to import the first and second file because the first file ip1_content = StringIO conatins 1.4 million ip and second file contains 700,253.

Output
{IPv4Address('192.168.0.799'), IPv4Address('192.168.0.800'), IPv4Address('192.168.0.900'), IPv4Address('1.117.250.1000')}
If we can output the file as newIPlist.txt instead of displaying it in terminal would be just great.
Only Ip address needed not this text IPv4Address possible?
Reply


Messages In This Thread
Match and extract if found - by Calli - Sep-07-2022, 05:00 PM
RE: Match and extract if found - by Gribouillis - Sep-07-2022, 06:27 PM
RE: Match and extract if found - by menator01 - Sep-07-2022, 06:33 PM
RE: Match and extract if found - by Calli - Sep-08-2022, 03:49 AM
RE: Match and extract if found - by perfringo - Sep-08-2022, 04:08 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 08:04 AM
RE: Match and extract if found - by DeaD_EyE - Sep-08-2022, 08:30 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 09:13 AM
RE: Match and extract if found - by DeaD_EyE - Sep-08-2022, 11:45 AM
RE: Match and extract if found - by Calli - Sep-08-2022, 05:54 PM
RE: Match and extract if found - by DeaD_EyE - Sep-09-2022, 09:46 AM
RE: Match and extract if found - by Calli - Sep-12-2022, 06:08 AM
RE: Match and extract if found - by DeaD_EyE - Sep-12-2022, 06:37 PM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:47 AM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:51 AM
RE: Match and extract if found - by Gribouillis - Sep-13-2022, 06:54 AM
RE: Match and extract if found - by Calli - Sep-13-2022, 06:56 AM
RE: Match and extract if found - by DeaD_EyE - Sep-14-2022, 08:37 AM
RE: Match and extract if found - by Pedroski55 - Sep-14-2022, 09:32 AM
RE: Match and extract if found - by DeaD_EyE - Sep-14-2022, 11:27 AM
RE: Match and extract if found - by Pedroski55 - Sep-16-2022, 06:05 AM
RE: Match and extract if found - by Gribouillis - Sep-16-2022, 07:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,439 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  If match not found print last line tester_V 2 3,020 Apr-26-2021, 05:18 AM
Last Post: tester_V
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,676 Nov-23-2020, 05:15 PM
Last Post: cananb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020