ループ内のコメントが正しいかどうかを把握しようとしています。私が望んでいるように、変数「デバイス」は「リストのリスト」になりますか? その場合、device[0][0] を使用してデータを呼び出すことはできますか? または、3 番目の行と 2 番目の項目が必要な場合は、device[2][1] を使用しますか?
def deviceFile():
devFile = raw_input('Enter the full file path to your device list file: \n-->')
open(devFile, 'r')
device = []
for line in devFile:
# creating an array here to hold each line. Call with object[0][0]
device.append(line.split(','))
return(device)
編集:
def deviceFile():
'''This def will be used to retrieve a list of devices that will have
commands sent to it via sendCommands(). The user will need to supply
the full file path to the raw_input(). This file will need to be a csv,
will need to have column0 be the IP address, and column1 be the
hostname(for file naming purposes). If a hostname is not supplied in
column1, the file will be named with the IP address.'''
devFile = raw_input('Enter the full file path to your device list file: \n-->')
thisFile = open(devFile, 'r')
device = []
for line in thisFile:
# creating an array here to hold each line. Call with object[0][0]
device.append(line.split(','))
thisFile.close()
return(device)
これは、「自分のコードは完璧か」というよりも、「論理的にこれを行っているか」というタイプの質問です。csvの各行を独自のリストにして、メインプログラムで呼び出すことでアクセスできるようにしたい:
デバイス = deviceFile()
マシン = デバイス[0][0]
最初の行の最初の項目を返します
マシン = デバイス[2][1]
3 行目の 2 番目の項目を返します