I am trying to import a csv of switch port configs.
I am able to print the csv from the below script
def read_csv_to_dict_list(filename):
data = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
return data
data = read_csv_to_dict_list("C:\\temp\\Sample Switch.csv")
# Access data
for row in data:
print(f"Port: {row['portId']}, Name: {row['name']}, Tags: {row['tags']}, Type: {row['type']}, Vlan: {row['vlan']}, VoiceVlan: {row['voiceVlan']}")"
how do I use this with "updateDeviceSwitchPort"
Solved! Go to solution.
!!UPDATE!!
I got it to work.
I added an if statement to replace empty cells with None
def read_csv_to_dict_list(filename):
data = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['Port_voiceVlan'] == '':
row['Port_voiceVlan'] = None
data.append(row)
return data
Take a look on those links.
https://github.com/meraki/automation-scripts
https://github.com/meraki/automation-scripts/blob/master/update_ports.py
You'll need to reference each field, individually.
Based on your print statement, something like
destSerial = 'Q2QN-9J8L-SLPD'
for row in data:
response = dashboard.switch.updateDeviceSwitchPort(
destSerial, row['portId'],
name=row['name'],
tags=row['tags'],
type=row['type'],
vlan=row['vlan'],
voiceVlan=row['voiceVlan']
)
Thank you.
I get an error
line 41, in <module>
serial, Port=row["portId"],
~~~^^^^^^^^^^
KeyError: 'portId'
** I changed the serial line. I have the SN as a variable already
The error message KeyError: 'portId' indicates that the key 'portId' is not found in the dictionary row. The CSV file does not have a column named 'portId'. You need to check the CSV and ensure that the column names match the keys you are using in your code.
line 1 of the csv is Port,name,tags,type,vlan,voiceVlan
**Edit** I updated the csv. was getting confusing.
If I print the dictionary keys..
dict_keys(['Port_num', 'Port_name', 'Port_tag', 'Port_type', 'Port_vlan', 'Port_voiceVlan'])
I figured it out, kinda
I had the dict keys swapped with the update switch API requirements
response = dashboard.switch.updateDeviceSwitchPort(
Serial, portId=row['Port_num'],
name=row['Port_name'],
type=row['Port_type'],
vlan=row['Port_vlan'],
)
I removed tags as it wants an array, and voiceVlan as it must be an integer or null
!!UPDATE!!
I got it to work.
I added an if statement to replace empty cells with None
def read_csv_to_dict_list(filename):
data = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['Port_voiceVlan'] == '':
row['Port_voiceVlan'] = None
data.append(row)
return data