[Tutor] converting tab-delimited text files to csv

Kent Johnson kent37 at tds.net
Sat Jan 27 04:39:17 CET 2007


Switanek, Nick wrote:
> I have a long tab-delimited text file that I’d like to convert into csv 
> format so I can read it into a statistics package.

Are you sure the statistics package can't read tab-delimited data 
directly? For example in R you can use read.delim().

> Here’s what I’ve tried to do:
> 
>  
> 
> import csv
> 
> inputFileList = file(‘input.txt’).readlines()
> 
> writer = csv.writer(file(‘output.csv', 'wb'))
> 
> writer.writerows(inputFileList)
> 
> del writer
> 
> When I try to open the resulting file in Excel (I’ve used a sample file 
> of 100 rows), however, it appears that I’m delimiting every character, 
> as each character has its own column. The rows in the original text file 
> are combinations of integers and character strings (some of which 
> include double quotes). Do I need to alter the ‘dialect’ or what?

The argument to writerows() should be a list of sequences. You are 
passing a list of strings, so the strings are interpreted as sequences 
of characters.

You need to divide the input rows at the tabs. You will also want to 
strip the trailing newlines off the input lines. Try this:

inputFileList = [ line.rstrip('\n').split('\t') for line in 
open('input.txt') ]

Kent



More information about the Tutor mailing list