从电子表格中抓取打印机IP和打印机名称

时间:2017-04-18 16:57:45

标签: python python-2.7 parsing spreadsheet

我需要从电子表格中获取打印机IP地址和打印机名称。此电子表格的外观如下:

Printer Model    Printer Name          Current IP       MAC     New IP
some printer     XRX0000AAF3230C       166.96.64.51     sad     127.0.1.1
example          NPIB36BA6             172.18.25.126    sad     255.255.255.0

如何获取IP地址和打印机名称,并将其插入dict?我已经能够使用xlrd使用以下代码打开并读取文件:

book = xlrd.open_workbook(r"C:\Users\Documents\My Forms\Printers 3-14-2017.xlsx", encoding_override="cp1252")
sheet = book.sheet_by_name("3rd Floor")
cols = sheet.ncols - 1
rows = sheet.nrows - 1
curr_row = -1
while curr_row < rows:
    curr_row += 1
    row = sheet.row(curr_row)
    print("Row: {}".format(curr_row))
    curr_cell = -1
    while curr_cell < cols:
        curr_cell += 1
        cell_type = sheet.cell_type(curr_row, curr_cell)
        cell_val = sheet.cell_value(curr_row, curr_cell)
        try:
            print("Type: {}\nValue: {}\n".format(cell_type, cell_val))
        except UnicodeEncodeError:
            print("Type: {}\nValue: {}\n".format(smart_str(cell_type), smart_str(cell_val)))

我需要做的是获取打印机名称和IP地址,将它们插入到dict中并输出该dict,如何成功完成此操作并跳过其他信息,以及“新IP”部分?

我考虑使用正则表达式,这可以使用IP地址,但除了打印机类型之外,打印机名称中没有模式,例如Xerox打印机以XRX开头,HP打印机以{{{ 1}}或NP等。对此有任何帮助将非常感谢,谢谢。

1 个答案:

答案 0 :(得分:0)

  

问题:我需要做的就是抓住打印机名称和IP地址,将它们插入一个...

my_dict['printer'] = sheet.cell_value(curr_row, 2)
my_dict['IP address'] = sheet.cell_value(curr_row, 3)

列从左到右计算为1

相关问题