1

I have written the following to isolate a very specific part of a file:

for line in open('120301.KAP'):
    rec = line.strip()
    if rec.startswith('PLY'):
       print line

The output appears as such

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    

Ideally what I am hoping for is the output to create a CSV file with just the coordinates. The PLY/1, PLY/2, etc. does not need to stay.

Is this doable? If not, at least can the print statements result in a new text file with the same name as the KAP file?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
dpalm
  • 133
  • 1
  • 3
  • 15
  • 2
    Possible duplicate of [How to redirect 'print' output to a file using python?](http://stackoverflow.com/questions/7152762/how-to-redirect-print-output-to-a-file-using-python) – xandermonkey Aug 08 '16 at 15:05
  • It's quite different, read my question in its entirety – dpalm Aug 08 '16 at 17:53

4 Answers4

3

You can use the csv module:

import csv  

with open('120301.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    for line in open('120301.KAP'):
        rec = line.strip()
        if rec.startswith('PLY'):
            writer.writerow(rec.split(','))

In a similar way, the csv.reader can easily read records from your input file.
https://docs.python.org/3/library/csv.html?highlight=csv#module-contents

If you are using Python 2.x, you should open the file in binary mode:

import csv  

with open('120301.csv', 'wb') as file:
    writer = csv.writer(file)
    for line in open('120301.KAP'):
        rec = line.strip()
        if rec.startswith('PLY'):
            writer.writerow(rec.split(','))
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
warownia1
  • 2,771
  • 1
  • 22
  • 30
1

This is totally doable!

Here are a couple of links to some docs for writing/reading CSV:
https://docs.python.org/2/library/csv.html

You could also just make your own CSV with the regular file reading/writing functions.

file = open('data', rw)
output = open('output.csv', w)
file.write('your infos') #add a comma to each string you output?
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
alpha32
  • 37
  • 6
1

You could open the file at the beginning of your code and then just add a write statement after the print line.

Something like this:

target = open(filename, 'w')
for line in open('120301.KAP'):
    rec = line.strip()
    if rec.startswith('PLY'):
       print line
       target.write(line)
       target.write("\n") #writes a new line
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Jason Tham
  • 123
  • 1
  • 8
  • This doesn't seem to work? Not sure what filename is supposed to be defining? – dpalm Aug 08 '16 at 17:36
  • filename here is a variable (string), you should just pass it the name of the file you want to open, such as "file.csv" – Jason Tham Aug 08 '16 at 22:08
0

The simplest way is to redirect stdout to a file:

for i in range(10):
   print str(i) + "," + str(i*2)   

will output:

0,0
1,2
2,4
3,6
4,8
5,10
6,12
7,14
8,16
9,18

if you run it as python myprog.py > myout.txt the result go to myout.txt

joel goldstick
  • 4,393
  • 6
  • 30
  • 46