currently have following code prints desired lines .kap file.
f = open('120301.kap') line in f: if line.startswith('ply'): print line
this results in following output
ply/1,48.107478621032,-69.733975000000 ply/2,48.163516399836,-70.032838888053 ply/3,48.270000002883,-70.032838888053 ply/4,48.270000002883,-69.712824977522 ply/5,48.192379262383,-69.711801581207 ply/6,48.191666671083,-69.532840015422 ply/7,48.033358898628,-69.532840015422 ply/8,48.033359033880,-69.733975000000 ply/9,48.107478621032,-69.733975000000
my goal not have print these lines. i'd have csv file created named 120301.csv coordinates in there own columns (leaving ply/# behind). simple enough? i've been trying different import csv functions awhile now. can't seem anywhere.
step step, since looks you're struggling basics:
f_in = open("120301.kap") f_out = open("outfile.csv", "w") line in f_in: if line.startswith("ply"): # copy line new file # split columns, ignoring first 1 ("ply/x") _, col1, col2 = line.split(",") # format output outstring = col1 + "," + col2 + "\n" # , write out f_out.write(outstring) f_in.close() f_out.close() # bad practice, i'll
of course not best way this. there's reason have things csv
module.
import csv open("120301.kap") inf, open("outfile.csv", "wb") outf: reader = csv.reader(inf) writer = csv.writer(outf) row in reader: # if first cell of row starts "ply"... if row[0].startswith("ply"): # write out row, ignoring first column writer.writerow(row[1:]) # opening files using "with" context managers means don't have # remember close them when you're done them.
Comments
Post a Comment