我有一个列表,我正在尝试将数据注入其中。列出一个像这样
data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081', 'TECH2_5625', 'TECH2_4598', 'TECH2_1966', 'TECH2_2573', 'TECH2_1800', 'TECH2_1529']
我要创建的主列表将使用现有配置以这种方式打印。下面的打印内容不是预期的结果,因为您只能看到它仅在data2列表中添加了第一项。其次,当我尝试for循环时,我将所有data2数据混在一起在同一列表中,请参见data3。
config1 = ['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!']
data3=['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!',['interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!','interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP','description TECH2_1507','description TECH2_1189','description TECH2_4081','description TECH2_5625','description TECH2_4598','description TECH2_1966','description TECH2_2573','description TECH2_1800','description TECH2_1529', '!',
我已经尝试将其分割为data2上的for和len,但是当我这样做时,它将所有数据连续放置在该列表中。
for line1 in data1:
words1 = line1.split()
if len(words1) > 0:
local = ''.join(words1[0:1])
config1.append('interface ' + local)
config1.append('power inline static max 30000')
config1.append('spanning-tree portfast')
description = ''.join(data2[:1])
config1.append('description ' + description)
config1.append('!') # totally optional
hostname = None
预期的列表值应如下所示。
expected_config1 = ['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/7', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1507', '!', 'interface Gi1/0/11', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1189', '!', 'interface Gi3/0/35', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4081', '!', 'interface Gi3/0/41', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_5625', '!', 'interface Gi3/0/25', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4598', '!', 'interface Gi3/0/43', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1966', '!', 'interface Gi3/0/23', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_2573', '!', 'interface Gi3/0/19', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1800', '!', 'interface Gi3/0/39', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1529', '!']
谢谢
答案 0 :(得分:1)
问题出在0
的处理上,否则您的总体附加策略是正确的。
首先,您始终只提取data2
的第一个元素。一种解决方案是维护一个单独的计数器(此处为data2
),每次执行附加阶段时该计数器都会增加,从而跟踪要附加的kdt
元素,
data2
此打印:
data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081', 'TECH2_5625', 'TECH2_4598', 'TECH2_1966', 'TECH2_2573', 'TECH2_1800', 'TECH2_1529']
config1 = []
data1 = ['Gi3/0/13 some more text']*len(data2)
kdt = 0
for line1 in data1:
words1 = line1.split()
if len(words1) > 0:
local = ''.join(words1[0:1])
config1.append('interface ' + local)
config1.append('power inline static max 30000')
config1.append('spanning-tree portfast')
description = data2[kdt]
config1.append('description ' + description)
config1.append('!') # totally optional
hostname = None
kdt += 1
print config1
第二个问题是,通过使用['interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_HELP', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1507', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1189', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4081', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_5625', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_4598', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1966', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_2573', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1800', '!', 'interface Gi3/0/13', 'power inline static max 30000', 'spanning-tree portfast', 'description TECH2_1529', '!']
,您实际上是在创建一个包含一个字符串的列表,即data2[0:1]
的第一个元素。如果没有加入,这将导致尝试在此行中使用data2
连接字符串和列表,
+
我认为这是您看到的错误。通过按索引访问正确的元素,实际上是在检索字符串,因此不需要联接操作。