lundi 21 mars 2016

python read and process each line in file using while and for loop

Input: A .txt file contains lots of lines of '$GPGSV,1,1,01,01,,,26*7D'

Output: 26

import subprocess
import csv
from string import split

f = open('GPS_SNR.csv', 'wb')

while True:
    with open('EMNA_GPS.txt', 'r') as g:        
        for line in g: 
            tempstr = line.split(',')[7]
            GPSstr = tempstr.split('*')[0]
            print GPSstr
            GPS = float(GPSstr)
            print type(GPS)
            wr = csv.writer(f, dialect = 'excel')
        wr.writerow([GPS])
        g.close()

What happen: The infinite loop never stops...lets say there are 100 lines in the file; however, there are thousands of lines wrote into the .csv instead of 100 lines.

Should I use 'if = EOF' statement?

Aucun commentaire:

Enregistrer un commentaire