Python
CSV
String Manipulation
Data Processing
Array Conversion

Python csv string to array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

If you have CSV data as a Python string and want to turn it into an array-like structure, the right tool is usually the standard csv module. A plain split(",") works only for the simplest input and breaks as soon as quoted commas, embedded newlines, or escaping rules appear.

Use csv.reader for Correct Parsing

The csv module understands real CSV rules. For a whole CSV document stored in a string, wrap the string with io.StringIO and feed it to csv.reader.

python
1import csv
2import io
3
4data = """name,age,city
5Alice,30,Toronto
6Bob,28,Montreal
7"""
8
9rows = list(csv.reader(io.StringIO(data)))
10print(rows)

Output:

python
1[
2    ["name", "age", "city"],
3    ["Alice", "30", "Toronto"],
4    ["Bob", "28", "Montreal"],
5]

This gives you a list of rows, where each row is a list of field strings. That is usually what people mean by "CSV string to array" in Python.

Parsing a Single CSV Row

If the input is only one row rather than a multi-line document, you do not need StringIO. A one-element list is enough:

python
1import csv
2
3row = 'Alice,30,"New York, NY"'
4fields = next(csv.reader([row]))
5print(fields)

Output:

python
["Alice", "30", "New York, NY"]

Notice why split(",") would fail here: the comma inside the quoted city name is data, not a delimiter.

Why split(",") Is Not a CSV Parser

This naive approach looks tempting:

python
row = 'Alice,30,"New York, NY"'
print(row.split(","))

But the result is wrong because CSV allows quoted fields:

python
['Alice', '30', '"New York', ' NY"']

Real CSV parsing has to understand:

  • delimiters
  • quotes
  • escaped quotes
  • line endings

That is exactly what csv.reader is for.

Converting Values After Parsing

The csv module returns strings. If you need numbers or booleans, convert them yourself after parsing.

python
1import csv
2import io
3
4data = """name,age
5Alice,30
6Bob,28
7"""
8
9reader = csv.DictReader(io.StringIO(data))
10records = [
11    {"name": row["name"], "age": int(row["age"])}
12    for row in reader
13]
14
15print(records)

Output:

python
1[
2    {"name": "Alice", "age": 30},
3    {"name": "Bob", "age": 28},
4]

If you need column names, DictReader is often more convenient than raw row arrays.

Custom Delimiters and Other Dialects

Not all delimited text uses commas. Some files use semicolons, tabs, or other separators. The csv module lets you specify the delimiter explicitly.

python
1import csv
2import io
3
4data = "Alice;30;Toronto\nBob;28;Montreal\n"
5rows = list(csv.reader(io.StringIO(data), delimiter=";"))
6print(rows)

For tab-separated data:

python
rows = list(csv.reader(io.StringIO(data), delimiter="\t"))

This is another reason to prefer csv.reader over manual string splitting. The parser is configurable without changing the overall structure of your code.

When pandas Makes Sense

If the CSV string represents a real table you plan to analyze, a DataFrame may be more useful than a list of lists.

python
1import pandas as pd
2import io
3
4data = "name,age\nAlice,30\nBob,28\n"
5df = pd.read_csv(io.StringIO(data))
6print(df)

You can always convert later:

python
rows = df.values.tolist()

Use this when your next steps are filtering, grouping, aggregation, or model preparation. If you just need parsed rows, the standard library is lighter and simpler.

Handling Embedded Newlines

One place where CSV parsing really earns its keep is multi-line quoted fields:

python
1import csv
2import io
3
4data = 'name,notes\nAlice,"line one\nline two"\n'
5rows = list(csv.reader(io.StringIO(data)))
6print(rows)

A manual line-by-line parser will often get this wrong. csv.reader handles it correctly because it parses according to CSV rules rather than assuming one physical line always equals one logical record.

Common Pitfalls

The most common mistake is using split(",") and assuming the data is simple enough forever. That breaks as soon as a field contains a quoted comma.

Another issue is expecting parsed numeric values to come back as numbers automatically. The standard csv module returns strings, so type conversion is your job.

Developers also sometimes forget that CSV dialects vary. If the file uses semicolons or tabs, configure the delimiter instead of rewriting the parser manually.

Finally, if the input has headers, decide early whether you want positional rows with reader or named rows with DictReader. Both are correct, but they fit different code styles.

Summary

  • Use csv.reader to parse a CSV string into a list of row arrays.
  • Wrap multi-line CSV strings with io.StringIO.
  • Use next(csv.reader([row])) for a single CSV row.
  • Avoid split(",") because it does not handle quoted CSV correctly.
  • Convert field types explicitly after parsing, or use DictReader or pandas when the structure calls for it.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.