Last modified: Nov 10, 2024 By Alexander Williams
Python csv.QUOTE_NONNUMERIC: Handle Mixed Data Types in CSV
When working with CSV files containing mixed numeric and text data, Python's csv.QUOTE_NONNUMERIC
provides a smart quoting strategy that automatically handles different data types appropriately.
Understanding csv.QUOTE_NONNUMERIC
Unlike csv.QUOTE_MINIMAL or csv.QUOTE_ALL, csv.QUOTE_NONNUMERIC
specifically quotes non-numeric fields while leaving numeric values unquoted.
Writing CSV Files with QUOTE_NONNUMERIC
import csv
data = [
['Name', 'Age', 'City'],
['John', 25, 'New York'],
['Alice', 30, 'London'],
]
with open('example.csv', 'w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_NONNUMERIC)
writer.writerows(data)
The resulting CSV file will automatically quote text fields while leaving numbers unquoted:
"Name","Age","City"
"John",25,"New York"
"Alice",30,"London"
Reading CSV Files with QUOTE_NONNUMERIC
Important: When reading files with csv.QUOTE_NONNUMERIC
, unquoted fields are automatically converted to float numbers.
import csv
with open('example.csv', 'r') as file:
reader = csv.reader(file, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
print(row, type(row[1]))
['Name', 'Age', 'City']
['John', 25.0, 'New York']
['Alice', 30.0, 'London']
Error Handling
When using csv.QUOTE_NONNUMERIC
for reading, ensure all numeric values are unquoted, or you'll encounter ValueError exceptions.
# This will raise an error
data = '"Name","25","City"' # Number is quoted
reader = csv.reader([data], quoting=csv.QUOTE_NONNUMERIC)
list(reader) # ValueError: could not convert string to float: '25'
Integration with DictWriter
You can also use csv.QUOTE_NONNUMERIC
with DictWriter for dictionary-based CSV operations.
Conclusion
csv.QUOTE_NONNUMERIC
is ideal for handling mixed data types in CSV files, automatically managing quotation marks and type conversion. Just be careful with quoted numeric values when reading files.