我维护一个excel工作表( csv文件),用于在设备“Product”、“Direction”、“”、“Port(Int)”、“Port(十六进制)”、“十六进制”、“十六进制”、“输入端口”、“输出端口”、“-description”、“启用的”、“-ethernetType”、“-inPort”、“-ipProtocol”、“-匹配类型”、“序号Output”、“Ipv4 Src”、“ipv4 Dest”、“Ipv6 Src”上进行配置。“Ipv6 Dest”、“TCP端口”、“TCP端口”
我不能带一个循环,遍历列表并使用每个单元格的键名。
发布于 2016-02-23 04:36:44
你试过python的csv
库吗?它具有将csv文件读取为列表列表所需的所有方法。使用它,您可以很容易地解决您的问题。
发布于 2016-02-23 04:37:00
您可以使用DictReader。您可以将CSV中的值作为python字典使用。
import csv
file_handle = open("filename.csv", "r")
reader = csv.DictReader(file_handle)
for row in reader:
configure(row['direction'], row['speed'], row['port'])
您可以使用CSV方言来处理excel文件(参考:https://pymotw.com/2/csv/#dialects)
根据下面的注释,我们可以将dict读取器转换为一个列表,并按如下索引访问list元素:
import csv
file_handle = open("filename.csv", "r")
# convert the dictreader dictionary to a list of dictionaries
reader = list(csv.DictReader(file_handle))
configure(reader[0]['direction'], reader[0]['speed'], reader[0]['port'])
# ... and so on. (or you can use a loop here the way you want)
https://stackoverflow.com/questions/35568822
复制相似问题