Reputation: 119
I have a list that im trying to inject data into. List one looks like this
data2 = ['TECH2_HELP', 'TECH2_1507', 'TECH2_1189', 'TECH2_4081', 'TECH2_5625', 'TECH2_4598', 'TECH2_1966', 'TECH2_2573', 'TECH2_1800', 'TECH2_1529']
The master list I am trying to make will be printed like this with my existing config. This below print is not the anticipated outcome, as you can see its only adding the first item in the data2 list. Secondly when i try a for loop I get all the data2 data mashed together in the same list see 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', '!',
Ive tried dividing this up doing a for and len on data2 but when i do that it places all the data in that list in a row.
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
the expected list value should look like the below.
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', '!']
Thank you
Upvotes: 1
Views: 440
Reputation: 4744
The problem was the handling of data2
, otherwise your overall appending strategy was correct.
First, you were always fetching only the first element of data2
. One solution for this is to maintain a separate counter (here kdt
) that will increase every time you execute the appending phase thus keeping track of which data2
element to append,
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
This prints:
['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', '!']
Second issue was that by using data2[0:1]
you were actually creating a list with one string, the first element of data2
. Without joining, this would cause an attempt to concatenate a string and a list with +
in this line,
config1.append('description ' + description)
which I assume was the error you were seeing. By accessing the correct elements by their index, you're actually retrieving a string, so the joining operation is not needed.
Upvotes: 2