简体   繁体   中英

Python parsing GPRMC string with CSV

I have GPRMC string which consists of 12 comma delimited values. When I run my code it does in fact split the commas, but it prints each character in the comma separated value on a new line - instead of grouping all characters in between a comma together.

For example:

>>> gprmc = "$GPRMC,1244.0,A,3111.334505,N,90729.3111898,W,1.2,,020811,,,A*55"
>>> gprmcReader = csv.reader(gprmc)
>>> for val in gprmcReader:
    print val


['$']
['G']
['P']
['R']
['M']
['C']
['', '']
['1']
['2']
['4']
['4']
['.']
['0']
['', '']
['A']
['', '']
['3']
['1']
['1']
['1']
['.']
['3']
['3']
['4']
['5']
['0']
['5']
['', '']

Try this:

import csv
reader = csv.reader(open(filename, 'r'))

for row in reader:
    if row and row[0].strip() == '$GPRMC':
        for val in row:
             print val
        print "_____________________"

To check my code i have created file that contains one row with the following text:

$GPRMC,135005.0,A,3526.351705,N,90729.337898,W,1.2,,020811,,,A*55

Executing my code prints me:

$GPRMC
135005.0
A
3526.351705
N
90729.337898
W
1.2

020811


A*55

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM