How to Fix CSV Error: sequence expected in Python
In this tutorial, you'll learn about How to Fix CSV Error: sequence expected in Python. We cover key concepts, practical examples, and best practices.
Python raises TypeError: sequence expected when the csv.writer receives a value that is not an iterable sequence while writing rows.
The Wrong Way
import csv
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow("not a list")
This produces:
Traceback (most recent call last):
File "script.py", line 4, in <module>
writer.writerow("not a list")
TypeError: sequence expected
Wait -- that actually writes each character as a separate cell. The real error:
import csv
data = [1, 2, 3]
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
This works fine. The actual "sequence expected" error occurs when passing a non-iterable like None:
import csv
data = None
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
This produces:
Traceback (most recent call last):
File "script.py", line 5, in <module>
writer.writerow(data)
TypeError: sequence expected
The Right Way
Ensure every row passed to writerow() or writerows() is an iterable:
import csv
rows = [
["Name", "Age", "City"],
["Alice", 30, "New York"],
["Bob", 25, "London"],
]
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
for row in rows:
if row is not None:
writer.writerow(row)
Step-by-Step Fix
1. Check for None values in data
import csv
data = [["Alice", 30], None, ["Bob", 25]]
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
for row in data:
if row is not None:
writer.writerow(row)
2. Use writerows with filtered data
import csv
data = [["Alice", 30], None, ["Bob", 25]]
valid_rows = [r for r in data if r is not None]
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(valid_rows)
3. Convert non-iterable values to strings
import csv
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow([str(None)]) # wrap non-iterables
Prevention Tips
- Always pass lists or tuples to
writerow()andwriterows(). - Filter out None values before writing CSV data.
- Use
csv.DictWriterfor dictionary-based data. - Validate data structure before CSV operations.
- Use a data validation library like
pydanticfor complex datasets.
Common Mistakes with csv error
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro