简体   繁体   中英

Fetching a IP from text file using python

I am creating custom code for fetching the IP and some other needed details from text file.

Consider the textfile is having following content: tenant_id and IP

cbdf25542c194a069464f69efff4859a 45.45.45.45
cbdf25542c194a069464f69efff4859a 1.6.7.3
cbdf25542c194a069464f69efff4859a 1.7.6.2
1235b3a73ad24b9c86cf301525310b24 2.3.7.5
1235b3a73ad24b9c86cf301525310b24 6.5.2.1

Now I have already created the code for fetching the IP and tenant separately.

Code is as follows:

files = open("/root/flattext", "r")

# create an empty list
ips = [] 
tenants = []

# read through the files
for text in files.readlines():

    # strip off the \n
    text = text.rstrip()

        # IP and Tenant Fetch
        regex = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})$', text)
        regex1 = re.findall(r'[0-9A-Za-z]{32}', text)

        if regex is not None and regex not in ips:
                ips.append(regex)

        if regex1 is not None and regex1 not in tenants:
                tenants.append(regex1)

ip_valuess = [''.join(ip) for ip in ips if ip]
tenant_ids = [''.join(tenant) for tenant in tenants if tenant]

# cleanup and close files
files.close()

So It will be giving result which consists of IP and Tenant_id as separate list.

Here what I need is fetching the IP which is coming under particular tenant ID.

Consider 1235b3a73ad24b9c86cf301525310b24 as a tenant_id, So it should be giving result as 2.3.7.5 , 6.5.2.1.

Some one please have a look and give me a better way to sort it out.

Why use regex just split works fine just use defaultdict -

from collections import defaultdict
data = defaultdict(list)
with open(r"D:\ip.txt",'rb') as fl:
    for i in fl.readlines():
        i=i.strip()
        data[i.split(" ")[0]].append(i.split(" ")[1])
print data.items()

Output-

[('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']), ('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])]

If your file is not structured and no space to split with then try regex -

import re
from collections import defaultdict
data = defaultdict(list)
pattern_ip = r'([\d]{1,3}(?=\.|$))'
pattern_tenat = r'^[a-z0-9]{32}'

with open(r"D:\ip.txt",'rb') as fl:
    for i in fl.readlines():
        i=i.strip()
        ip = '.'.join(re.findall(pattern_ip,i))
        tent = ''.join(re.findall(pattern_tenat,i))
        data[tent].append(ip)
print data.items()

Output-

[('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']), ('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])]

See regex LIVE DEMOTENANT and DEMOIP

Use split and defaultdict :

from collections import defaultdict

results = defaultdict(list)

with open('flattext', 'r') as f:
    for row in f.read().strip().split('\n'):
        if row.strip() != "":
            tenant_id, ip = row.split()
            results[tenant_id].append(ip)

print results.get('1235b3a73ad24b9c86cf301525310b24', None)
print results.items()

Output:

['2.3.7.5', '6.5.2.1']

And results content:

[
  ('1235b3a73ad24b9c86cf301525310b24', ['2.3.7.5', '6.5.2.1']),
  ('cbdf25542c194a069464f69efff4859a', ['45.45.45.45', '1.6.7.3', '1.7.6.2'])
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM