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']}")"
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
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.
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.