私は新しいプログラマーで、新しい辞書名をパラメーターとして関数に渡すことに問題があります。
Webページからデータを取得し、ホスト名の辞書キーとデータの全行の値を作成する関数を作成しようとしています. ホスト名をキー値とする共通性を持つページが複数あり、最終的にはそれらを 1 つの行に結合します。
まず、control
探しているすべてのホストのキー ファイルとして使用されるというリストを作成します。次に、値webpage
、delimiter
、およびdictionary name
を関数に渡します。
これを行うと、辞書の名前が関数に渡されていないようです。
#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)
出力は次のとおりです。
$./test.py
The length of osdata inside the function: 11
The length of osdata outside the function: 0
関数でキーワードが拾われていないようです。
これはスコープによるものですか?