0

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.

1
  • 2
    Regex is generally for matching / searching. You have a structured file format, why do you feel the need to use a regex? Just parse it, namely using split. Commented Nov 28, 2015 at 5:14

2 Answers 2

2

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

1

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'])
]
11
  • One more query. Consider this is a results content: defaultdict(<type 'list'>, {'1235b3a73ad24b9c86cf301525310b24': ['2.3.7.5', '6.5.2.1'], 'cbdf25542c194a069464f69efff4859a': ['45.45.45.45', '1.6.7.3', '1.7.6.2']}) In this one I need to check whether IP 2.3.7.5 is available for tenant 1235b3a73ad24b9c86cf301525310b24. How can I do this?
    – iamnewuser
    Commented Nov 28, 2015 at 11:01
  • Whether is it good to go.? ip_list = results.get('1235b3a73ad24b9c86cf301525310b24', None) if ip in ip_list: print "Yes it is" else: print "Sorry no entry"
    – iamnewuser
    Commented Nov 28, 2015 at 11:07
  • @iamnewuser, I think you have asked in the wrong answer. The one you selected is the above one Commented Nov 28, 2015 at 18:33
  • Ok Thanks , I have one more query
    – iamnewuser
    Commented Nov 30, 2015 at 9:09
  • I am using your code sample. It is working fine. But some times it is resulting in error like this: tenant_id, ip = row.split() ValueError: need more than 0 values to unpack
    – iamnewuser
    Commented Nov 30, 2015 at 9:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.