User100696
User100696

Reputation: 707

Python output in regular expressions

Hey community I'm new in python and I have a question may be this have been answered before but I want to know if that's possible

I have this python code:

import re

file = open("address.txt","r")
content = file.read()
file.close()
content = content.split('LAN ')[1:]
dic = {}
for lan in content:
    dic[int(lan[0])] = lan[1:] 



def address(lan_index):
    address = re.findall('address\s(.*?)\s',dic[lan_index] )
    print 'LAN',lan_index,":",address
    return address
address(1)

where my output is:

LAN 1 : ['192.168.0.0']

Is it possible to remove [''] and print only the address? for example something like this:

LAN 1 : 192.168.0.0 <--- That's the output I want.

Upvotes: 0

Views: 47

Answers (1)

Uriel
Uriel

Reputation: 16184

address is the result of a re.findall so it comes in the form of a list. If you want the (only) result that is supposed to be returned take the first item from that list:

print 'LAN', lan_index, ":", address[0]

I would also suggest to make sure an address was found:

def address(lan_index):
    address = re.findall('address\s(.*?)\s',dic[lan_index] )
    if len(address) > 0:
        print 'LAN', lan_index, ":", address[0]
    else:
        print 'No address was found!'
    return address

Upvotes: 1

Related Questions