我需要在csv文件中使用python组合行($GPGGA
和$GPVTG
)并遇到问题。 csv文件如下所示:
~11:16:04.831,$GPGGA,091606.00,5149.28020915,N,01140.54074205,E
~11:16:04.861,$GPVTG,40.8,T,,,000.05,N,000.09,K,D*75
~11:16:05.833,$GPGGA,091607.00,5149.28020818,N,01140.54074319,E
~11:16:05.863,$GPVTG,40.8,T,,,000.01,N,000.01,K,D*79
我试过
eingabe = sys.argv[1]
with open(eingabe, "rb") as file:
datareader = csv.reader(file)
for row in datareader:
if "$GPGGA" in row:
col1 = row[0], row[1], row[3], row[5]
if "$GPVTG" in row:
col2 = row[0], row[1], row[2]
print col1 + col2
结果只是最后一行,但我需要所有这些
('~11:16:08.827','$ GPGGA','5149.28021200','01140.54075064','~11:16:08.867','$ GPVTG','40 .8)
我如何实现这一目标?
答案 0 :(得分:0)
>>> with open('1.csv') as file:
... reader = csv.reader(file)
... col1s = []; col2s = []
... for row in reader:
... if "$GPGGA" in row:
... col1s.append((row[0], row[1], row[2], row[5]))
... elif "$GPVTG" in row:
... col2s.append((row[0], row[1], row[2]))
... for each in zip(col1s, col2s):
... print each[0] + each[1]
...
('~11:16:04.831', '$GPGGA', '091606.00', '01140.54074205', '~11:16:04.861', '$GPVTG', '40.8')
('~11:16:05.833', '$GPGGA', '091607.00', '01140.54074319', '~11:16:05.863', '$GPVTG', '40.8')
>>>