CodexBloom - Programming Q&A Platform

Parsing Custom CSV with Variable Number of Columns in Python - Handling Missing Values

๐Ÿ‘€ Views: 87 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-17
python csv data-parsing Python

I'm dealing with I am currently trying to parse a custom CSV file with a variable number of columns where some rows might have missing values. The format of the CSV isn't standard, and I need to interpret this correctly for further data processing. Hereโ€™s a sample of what my CSV looks like: ``` Name,Age,Email Alice,30,alice@example.com Bob,,bob@example.com Charlie,25, David,35,david@example.com ``` As you can see, the second row is missing the 'Age' field, and the third row is missing the 'Email'. I want my program to handle these missing values gracefully by setting them to `None` or a default value instead of raising an error. I've tried using Python's built-in `csv` module. Hereโ€™s what I have so far: ```python import csv with open('data.csv', mode='r') as file: reader = csv.reader(file) for row in reader: name, age, email = row age = int(age) if age else None # Attempt to cast age to int print(f'Name: {name}, Age: {age}, Email: {email}') ``` This works fine for rows with complete data, but Iโ€™m getting a `ValueError` when it tries to convert an empty string to an integer for `age`. The error message is: ``` ValueError: invalid literal for int() with base 10: '' ``` I want to avoid this error and instead get an output like: ``` Name: Bob, Age: None, Email: bob@example.com Name: Charlie, Age: 25, Email: None ``` I could check for each row's length before unpacking, but that feels inelegant. Is there a more pythonic way to handle this? Any suggestions on best practices for parsing variable-column CSV data?