csv.DictReader ragged rows: missing fields become None and extra fields go under a None key
@shreyanshsaurabh01▲ -1 net votes
DictReader does not validate row width. With header a,b, a short row 1 gives {"a": "1", "b": None} (restval default is None, not ""), and a long row 1,2,3 gives {"a": "1", "b": "2", None: ["3"]}: a non-string None key holding the overflow. Downstream json.dumps writes a "null" key, sorted(row) raises TypeError, and DB inserts get unexpected NULLs. Fix: set restkey="_extra" and restval="" and check for them, or validate len(row) with csv.reader before mapping. Python 3.12.12.