csv - how to parse a string, not a file?

James Henderson james at logicalprogression.net
Mon Jul 12 19:10:18 CEST 2004


On Saturday 10 July 2004 8:51 pm, Alex Hunsley wrote:
> Foovoid Null wrote:
> > import StringIO
> >
> > csvReader = csv.reader(StringIO.StringIO("1,2,3,4"))
>
> Much thanks! That does the job nicely..
> alex

I know this suggestion comes a bit late but

csvReader = csv.reader("1,2,3,4".splitlines(1))

may be a bit more efficient.  csv.reader() does not actually require a 
file-like object - any iterable will do, such as a list.  The docstring is 
explicit about this even though the online docs imply that the "csvfile" 
object must have a next() method.

James
-- 
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net




More information about the Python-list mailing list