Greetings, I'm a python newbie struggling with the obvious. I have a txt file with fixed width whitespace between the elements. Excel can't import directly because the size of the whitespace varies between columns. I have stripped the header out leaving only data.
This is the code I've tried to date, which up to a point gives me a beautiful list 'out' that is comma separated. Trying to write that to a clean .csv is proving fruitless. Any suggestions?
import sys
import csv
input = open(sys.argv[1])
lines = 0
hits = 0
out = list()
while 1:
record = input.readline()
if record == "": break
lines += 1
out.extend(record.split())
#print record,
hits += 1
print "Processed",hits,"out of",lines
#writer = csv.writer(open("f:/CalcOut.csv", "wb"))
#writer.writerows(out)
#writer.close()
sample of 'out' list:
How to get the list above into csv is the real question?
What command to use to close the output file?
input style:
0.125000 790101 0300 1.036 77.7 69.0 0.2100 0.1919 4.611 5.210 22.3 0.2533 4.201 80.5 4.143 0.692 2.4472e+003 77.5 2.3896e+003 5.2775e+002 2.1319e+003 1.2015e+003 2.4524e+003 2.1446e+003 1.1897e+003 1.031 4.603 2.5005e+003 2.2698e+003 1.0489e+003 0.974 4.537
That's one line of input data, consisting of 32 elements. My sample file has 10 lines, my real work has 59,000 lines. It will just fit into excel for further analysis.
Cheers
Caraka