[Tutor] Using the 'csv' module to parse tab-delimited data

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri May 7 14:06:00 EDT 2004



On Thu, 6 May 2004, Tim Johnson wrote:

>     I am importing TAB-delimited text data.


Hi Tim,


Glad that you found the problem (stripping the "empty" record at the end)!


The Standard Library has a nice 'csv' library that takes care of parsing
delimited lines:

    http://www.python.org/doc/lib/module-csv.html



For example:

###
>>> import csv
>>> from StringIO import StringIO
>>> sample_file = StringIO("this\tis\ta\ttest\ndo\tyou\tsee\tthis?\n")
>>> reader = csv.reader(sample_file)
>>> for row in reader:
...     print row
...
['this\tis\ta\ttest']
['do\tyou\tsee\tthis?']
###


Ooops!  I forgot that the default delimiter in 'csv' is the comma
character.  Let me fix that... let's see what other kind of dialects are
supported by 'csv'.

###
>>> csv.list_dialects()
['excel-tab', 'excel']
###



Ok, let me switch the mode to 'excel-tab'; that sounds like it should use
tabs as the column delimiters:

###
>>> reader = csv.reader(sample_file, dialect='excel-tab')
>>> sample_file.seek(0)
>>> for row in reader:
...     print row
...
['this', 'is', 'a', 'test']
['do', 'you', 'see', 'this?']
###


Better.  *grin*


Hope this helps!




More information about the Tutor mailing list