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.
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.
Output:
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:
Output:
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:
But the result is wrong because CSV allows quoted fields:
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.
Output:
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.
For tab-separated data:
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.
You can always convert later:
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:
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.readerto 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
DictReaderorpandaswhen the structure calls for it.
Related reading
- Python data structure sort list alphabetically
- Python dataclass from a nested dict
- Python dictionary are keys and values always the same order?
- Python Dictionary Comprehension
- python dataframe pandas drop column using int
- python date of the previous month
- Python dictionary from an object's fields
- Python Dijkstra k shortest paths

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 courseTrack 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.