views:

166

answers:

3

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.

Thank you

+6  A: 

I would use StringIO:

import StringIO
import csv

scsv = """1,2,3
a,b,c
d,e,f"""

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

simplier version with split() on newlines:

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

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.

Michał Niklas
the split method wouldn't work if his csv file contained strings which contained commas
Carson Myers
or quoted strings as values (with or without commas)
adamk
Very awesome! thank you so much for that answer!
Drew LeSueur
+1  A: 

Simple - the csv module works with lists, too:

>>> a=["1,2,3","4,5,6"]  # or a = "1,2,3\n4,5,6".split('\n')
>>> import csv
>>> x = csv.reader(a)
>>> list(x)
[['1', '2', '3'], ['4', '5', '6']]
adamk
+1  A: 

As others have already pointed out, Python includes a module to read and write CSV files. It works pretty well as long as the input characters stay within ASCII limits. In case you want to process other encodings, more work is needed.

The Python documentation for the csv module implements an extension of csv.reader, which uses the same interface but can handle other encodings and returns unicode strings. Just copy and paste the code from the documentation. After that, you can process a CSV file like this:

with open("some.csv", "rb") as csvFile: 
    for row in UnicodeReader(csvFile, encoding="iso-8859-15"):
        print row