Fix Python – Python csv string to array


Asked By – Drew LeSueur

Anyone know of a simple library or function to parse a csv encoded string and turn it into an array or dictionary?

I don’t think I want the built in csv module because in all the examples I’ve seen that takes filepaths, not strings.

Now we will see solution for issue: Python csv string to array


You can convert a string to a file object using io.StringIO and then pass that to the csv module:

from io import StringIO
import csv

scsv = """text,with,Polish,non-Latin,letters

f = StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:

simpler version with split() on newlines:

reader = csv.reader(scsv.split('\n'), delimiter=',')
for row in reader:

Or you can simply split() this string into lines using \n as separator, and then split() each line into values, but this way you must be aware of quoting, so using csv module is preferred.

On Python 2 you have to import StringIO as

from StringIO import StringIO


This question is answered By – Michał Niklas

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0