我试图弄清楚我在循环中的评论是否正确。变量“设备”会像我希望的那样成为“列表列表”吗?如果是这样,我可以使用 device[0][0] 调用数据吗?或者,假设我想要第三行和第二项,使用 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 的每一行都是它自己的列表,并且能够通过在我的主程序中调用它来访问它:
设备 = 设备文件()
机器=设备[0][0]
返回第一行的第一项
机器=设备[2][1]
返回第三行的第二项