Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Excel中的列读取数据_Python_Python 2.7_Loops - Fatal编程技术网

Python 从Excel中的列读取数据

Python 从Excel中的列读取数据,python,python-2.7,loops,Python,Python 2.7,Loops,我正在使用python脚本从Excel文件的列中读取数据,并使用这些数据在Cisco交换机上执行命令 例如,我有以下两列 Switch MAC aaa.com mac.aaa.000 bbb.com mac.bbb.000 ccc.com mac.ccc.000 ..... ..... python现在将转到switch aaa,并将mac.aaa.000的端口安全设置为sticky 这是命令: switchport port-secu

我正在使用python脚本从Excel文件的列中读取数据,并使用这些数据在Cisco交换机上执行命令

例如,我有以下两列

Switch        MAC
aaa.com      mac.aaa.000
bbb.com      mac.bbb.000
ccc.com      mac.ccc.000
.....        .....
python现在将转到switch aaa,并将mac.aaa.000的端口安全设置为sticky

这是命令:

switchport port-security **<mac-address>** sticky

请帮忙。循环将从文件中读取所有交换机和MAC,并执行sticky命令。

我一直在使用openpyxl将excel数据读取到脚本中

    from openpyxl import *
    from tkinter.filedialog import askopenfilename,askdirectory,asksaveasfilename
    #file selector using tkinter
    file = askopenfilename()
    #load the workbook
    wb=load_workbook(file)
    #get sheets
    sheets = wb.sheetnames
    #select sheet to use by index from sheetnames
    ws=wb[sheets[0]]
    #loop through the rows of worksheet and store in list
    switchMAC=[]
    for r in range (ws.max_row):
        row=[]
        for c in range(ws.max_column):
            row.append(ws.cell(row=r+1,column=c+1).value)
        switchMAC.append(row)
从这里您现在有了一个列表(switchMAC),您可以循环传递命令

    for r in switchMac:
        host=r[0]
        MAC=r[1]
        #add the rest of your code for each host within the loop
您可以使用此脚本将excel数据表转换为字典列表:

import xlrd

workbook = xlrd.open_workbook('foo.xls')
workbook = xlrd.open_workbook('foo.xls', on_demand = True)
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock the name of the column
for col in range(worksheet.ncols):
    first_row.append( worksheet.cell_value(0,col) )
# transform the workbook to a list of dictionaries
data =[]
for row in range(1, worksheet.nrows):
    elm = {}
    for col in range(worksheet.ncols):
        elm[first_row[col]]=worksheet.cell_value(row,col)
    data.append(elm)
print data
你也可以使用


您可以使用此代码从excel文件中读取数据

import os
import datetime
import sys
import re
import openpyxl
import uuid
from openpyxl import load_workbook

class ExcelDictSmall(dict):
    '''
    Dictionary with all Sheet Names as key, value is a List of Rows each item in a Row List is a List of Cells
    '''
    def __init__(self,dir,fname):
        fullFname = os.path.join(dir,fname)
        wb = load_workbook(filename=fullFname, read_only=True)
        allSheets= wb.get_sheet_names()
        for sheet in allSheets:
            sheetList = []
            rowlist=[]
            for row in wb[sheet].rows:
                for cell in row:
                    rowlist.append(cell.value)
                sheetList.append(rowlist)
                rowlist=[]
            self[sheet]=sheetList

我下载了openpyxl文件夹,但似乎找不到tkinter。我做了Tkinter,它认出了它,但“skopenfilename”等有红线。我的同事告诉我,不建议从excel读取数据,所以请您能够为一个简单的txt文件添加解决方案。它将有交换和耦合空间,然后是MAC地址,然后是下一行等等。在这一点上,您最好的选择是在文本(主机,MAC)编辑器中格式化为csv,并使用标准csv库和上述相同的for循环过程@Damon tkinter不是openpyxl库的一部分,它是一个不同的python包。
from pandas import *
xls = ExcelFile('foo.xls')
df = xls.parse(xls.sheet_names[0])
print df.to_dict()
import os
import datetime
import sys
import re
import openpyxl
import uuid
from openpyxl import load_workbook

class ExcelDictSmall(dict):
    '''
    Dictionary with all Sheet Names as key, value is a List of Rows each item in a Row List is a List of Cells
    '''
    def __init__(self,dir,fname):
        fullFname = os.path.join(dir,fname)
        wb = load_workbook(filename=fullFname, read_only=True)
        allSheets= wb.get_sheet_names()
        for sheet in allSheets:
            sheetList = []
            rowlist=[]
            for row in wb[sheet].rows:
                for cell in row:
                    rowlist.append(cell.value)
                sheetList.append(rowlist)
                rowlist=[]
            self[sheet]=sheetList