I'm a new programmer and am having issues with passing a new dictionary name as a parameter to a function.
I'm trying to create a function that will pull down data from a webpage and create a dictionary key for the hostname and a value of the full line of data. There are multiple pages that have the commonality of the hostname as a key value, which I will eventually join together in a single row.
First, I create a list called control
used as a key file of all the hosts I'm searching for. I then pass the values webpage
, delimiter
, and dictionary name
to the function.
When doing this, it seems the name of the dictionary is not being passed to the function.
#open key file
f = open("./hosts2", "r")
control = []
for line in f:
line = line.rstrip('\n')
line = line.lower()
m = re.match('(^[\w\d]+)', line)
control.append(m.group())
# Close key file
f.close()
def osinfo(url, delimiter, name=None):
ufile = urllib2.urlopen(url)
ufile.readline()
name = {}
for lines in ufile.readlines():
lines = lines.rstrip("\n")
fields = lines.split(delimiter)
m = re.match(r'(?i)(^[a-z0-9|\.|-]+)', fields[1].lower())
hostname = m.group()
if hostname in control:
name[hostname] = lines
print "The length of osdata inside the function:", len(name)
osdata = {}
osinfo(‘http://blahblah.com/test.scsv’, ';', name='osdata')
print "The length of osdata outside the function", len(osdata)
The output is as follows:
$./test.py
The length of osdata inside the function: 11
The length of osdata outside the function: 0
It seems that the keyword is not being picked up by the function.
Is this due to scope?