Python用CSV解析GPRMC字符串

时间:2011-08-02 16:02:02

标签: python parsing csv

我有GPRMC字符串,由12个逗号分隔的值组成。当我运行我的代码时,它确实会分割逗号,但它会在新行上以逗号分隔值打印每个字符 - 而不是将逗号之间的所有字符分组在一起。

例如:

>>> 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']
['', '']

1 个答案:

答案 0 :(得分:0)

试试这个:

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 "_____________________"

要检查我的代码,我创建了包含一行的文件,其中包含以下文本:

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

执行我的代码打印我:

$GPRMC
135005.0
A
3526.351705
N
90729.337898
W
1.2

020811


A*55