[Tutor] converting tab-delimited text files to csv

Switanek, Nick nswitanek at stanford.edu
Sat Jan 27 02:48:21 CET 2007


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. 

I've been using Excel to do the format conversion up till now, but now I
have more rows than Excel can handle, and would like to avoid going
through Excel if possible. 

 

I've found the csv module but am confused by its documentation, as I am
with the few threads on the subject generally. 

 

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?

 

Many thanks in advance for your help!

 

Nick

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070126/4a5e019c/attachment.htm 


More information about the Tutor mailing list