Importing data from tab-delimited files
Another very common format of flat datafile is the tab-delimited file. This can also come from an Excel export but can be the output of some custom software we must get our input from.
The good thing is that usually this format can be read in almost the same way as CSV files, as the Python module csv
supports so-called dialects that enable us to use the same principles to read variations of similar file formats—one of them being the tab delimited format.
Getting ready
We are already able to read CSV files. If not, please refer the Importing data from CSV recipe first.
How to do it...
We will re-use the code from the Importing data from CSV recipe, where all we need to change is the dialect we are using.
import csv filename = 'ch02-data.tab' data = [] try: with open(filename) as f: reader = csv.reader(f, dialect=csv.excel_tab) header = reader.next() data = [row for row in reader] except csv.Error as e: print "Error reading CSV...