[Tutor] converting tab-delimited text files to csv
rabidpoobear at gmail.com
Sat Jan 27 04:01:56 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.
> 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'))
> 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!
csv is comma-separated values, right?
you should be able to just do a string replace of tabs -> commas on each
line in the new file...
or is the csv format more complicated than that?
More information about the Tutor