Output to csv

Doran, Harold HDoran at air.org
Wed Jul 25 15:19:35 EDT 2007


I've hacked together a small utility program that I assume can be
written much better. The program reads in output from a statistical
program and spits out the relevant data needed. The purpose of the
program is to reach into the original data file (which is a text file),
pull out the relevant informaiton, and spit out a file that can be
opened in Excel in order to create tables for some reports we need.

To accomplish this, I enter in commas by brute force so that the output
is a csv file that excel can open. This program works fine, but, as I am
still learning python, I am interested in ways to write better code. If
anyone has any suggestions, they are most appreciated.

Below is the program in its current form.

Harold



filename = raw_input("Please enter the WinSteps ISF file: ")
new_file = raw_input("Enter the name of the csv file to output: ")

# create a new file defined by the user
f = open(new_file, 'w')

f.write("Num, Measure, SE, Measure, SE, Measure, SE, Measure, SE \n")

params = open(filename, 'r')

for line in params:
	x = line
	if x[15] == '1':
		print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ',,,'
        elif x[15] == '2':
		print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ','
,x[90:97], ',' ,x[99:105], ',,'
	elif x[15] == '3':
		print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ','
,x[90:97], ',' ,x[99:105], ',' ,x[140:147], ',' ,x[149:155], ','
	elif x[15] == '4':
		print >> f, x[4:6], ',' ,x[39:47], ',' ,x[49:55], ','
,x[90:97], ',' ,x[99:105], ',' ,x[140:147], ',' ,x[149:155],
',',x[190:197], ',' ,x[199:205]
		
f.close()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070725/64f84dc8/attachment.html>


More information about the Python-list mailing list