Python import csv API

Solved
Capt_Caaaveman
Getting noticed

Python import csv API

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"

 

1 Accepted Solution
Capt_Caaaveman
Getting noticed

!!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

 

View solution in original post

7 Replies 7
alemabrahao
Kind of a big deal
Kind of a big deal

Take a look on those links.

 

https://github.com/meraki/automation-scripts

 

https://github.com/meraki/automation-scripts/blob/master/update_ports.py

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
rhbirkelund
Kind of a big deal
Kind of a big deal

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']
    )
LinkedIn ::: https://blog.rhbirkelund.dk/

Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂

All code examples are provided as is. Responsibility for Code execution lies solely your own.
Capt_Caaaveman
Getting noticed

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

alemabrahao
Kind of a big deal
Kind of a big deal

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.

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
Capt_Caaaveman
Getting noticed

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'])

 

Capt_Caaaveman
Getting noticed

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

Capt_Caaaveman
Getting noticed

!!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

 

Get notified when there are additional replies to this discussion.