如何访问每行元素的值

时间:2019-04-30 16:23:10

标签: python-2.7 csv

我需要重新配置apc网络管理卡,因为它们已使用错误的IP进行配置。我已经测试了通过Netmiko与它们的连接,并且发现'cisco_ios_telnet'设备类型有效,并且我可以输入命令并将其发送到设备。 设备和更新的值在csv文件中列出。我的想法是将csv文件解析为行,然后使用NetMiko依次连接到每个文件,然后发出将更改其ip,掩码和网关的命令。

我想使用csv.DictReader(当然,如果合适的话) 因为我不了解熊猫,而只是看到能起作用的东西,所以忽略了熊猫的台词。

我的csv文件(apc.csv)看起来像这样 ID,IP,目标,掩码,GW 1.1.1.1,2.2.2.2,255.0.0.0,2.2.2.1 1.1.1.2,2.2.2.3,255.0.0.0,2.2.2.1

*#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import sys
import csv
import pandas as pd
import logging
from netmiko import ConnectHandler, Netmiko
from getpass import getpass

#df1 = pd.read_csv('apc.csv')

#df2 = df1.set_index("id", drop = False)

#print(df2[1:2])



with open('apc.csv') as csvfile:
    reader = csv.DictReader(csvfile,delimiter=" ")
    for row in reader:
        print(row['id'], row['ip'], row['target'], row['mask'], row['gw'])
        device = {
            'device_type': 'cisco_ios_telnet',
            'host': 'host',
            'username': 'user',
            'password': 'pass',
        }
net_connect = ConnectHandler(**device)
print("Connecting to device")
commands = ['tcpip -i[target] -s[mask] -g[gw]']

output = net_connect.send_command(commands)

net_connect.disconnect()*

#python apc_config_changes.py
Empty DataFrame
Columns: [id, ip, target, mask, gw]
Index: []
Traceback (most recent call last):
  File "apc_config_changes.py", line 24, in <module>
    reader = csv.DictReader(csvfile,delimiter=" ")
  File "/usr/lib/python2.7/csv.py", line 79, in __init__
    self.reader = reader(f, dialect, *args, **kwds)
TypeError: "delimiter" must be string, not unicode

我觉得我缺少了如何访问每个行和字段的值的概念

0 个答案:

没有答案