Import csv - null values

Im new to python. Im trying to use it for Meraki API’s. Im using Pycharm.

I set up a test csv with the data I want to change on the device.
Everything works except the voicevlan

[“‘voiceVlan’ must be an integer or null”]}

the csv has None as the data for that. I have tried ‘None’, None, null, and just double quotes (“”)

I was trying to add an if statement to change it, but I am not sure where to place it, or if this is even correct.

if row['Port_voiceVlan'] == 'None':
          row['Port_voiceVlan'] = None

I also tried a breakpoint to verify what the csv reader sees. Im not sure if I did it correctly.

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


breakpoint()

data = read_csv_to_dict_list("C:\\temp\\Sample Switch.csv")

print(data)

then in the debugger

> data = read_csv_to_dict_list("C:\\temp\\Sample Switch.csv")

(Pdb) c

[{'Port_num': '2', 'Port_name': 'XXX', 'Port_tag': 'XXX', 'Port_type': 'access', 'Port_vlan': '2', 'Port_voiceVlan': 'None'},

Here is the code:

import csv

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")

# Update switch port configs
for row in data:
     
     response = dashboard.switch.updateDeviceSwitchPort(
         Serial, portId=row['Port_num'],
         name=row['Port_name'],
         #tags=row['Port_tag'],
         type=row['Port_type'],
         vlan=row['Port_vlan'],
         voiceVlan=row['Port_voiceVlan'],
     )

CSV does not have null concept.
You will need to handle that case yourself.
You could leave the cell empty and then map an empty cell to None.

Thank you.

I researched your reply and updated my code and it WORKS!!

I added the 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