Skip to content

How to Fix CSV Error: sequence expected in Python

DodaTech Updated 2026-06-24 3 min read

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() and writerows().
  • Filter out None values before writing CSV data.
  • Use csv.DictWriter for dictionary-based data.
  • Validate data structure before CSV operations.
  • Use a data validation library like pydantic for complex datasets.

Common Mistakes with csv error

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. 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

### What types does writerow() accept?

writerow() accepts any iterable: lists, tuples, generators, and custom iterables. It does not accept scalar values like integers, strings (it iterates over characters), or None.

What is the difference between writerow and writerows?

writerow() writes a single row (one iterable). writerows() writes multiple rows (an iterable of iterables). Confusing them is a common source of errors.

Can I write dictionaries to CSV?

Yes, use csv.DictWriter to write dictionaries. Pass fieldnames to the constructor and use writerow(dict) or writerows(list_of_dicts). This is more readable for structured data.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro