The little csv module that could

The CSV module in python can process more than just CSV files. It can also handle tab separated ones.
In fact, it has an in-built sniffer to guess what type of delimiters the file is using. This includes guessing whether strings are quoted or not.

Here’s some sample code:

dialect=csv.Sniffer().sniff(open('fitted.txt', 'r').read())
for column_1, column_2, column_3 in \
   csv.reader(open('fitted.txt', 'r'), dialect=dialect):
      pass # do something here

One Response to “The little csv module that could”

  1. krys.ca writes:

    Python CSV module has a delimiter sniffer…

    According to Chui , Python’s built-in CSV module has a delimiter sniffer .  I did not know this, but I can probably make good use of it at work.   I’ll have to play with it….

Leave a Reply