ppap
ppap

Reputation: 331

csv.DictReader method loads empty cells with empty string

I'm running into the issue where python DictReader is loading the dictionary, but it is loading the empty csv cell as a empty string. I want it to load it as a None value.

Is there a fix for this?

Upvotes: 2

Views: 2096

Answers (1)

BoarGules
BoarGules

Reputation: 16941

What you describe is how the csv module is designed to work.

The only fix is for you to write code to bring the data into line with what you want after reading it. Like this:

with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        myfixedrow = {k: (None if v == "" else v) for k,v in row.items()}

        

Upvotes: 6

Related Questions