0

在 python 脚本中,我有 2 个浮动值和 2 个列表,我想在 csv 文件中写入。longwgs84 和 latwgs84 是浮点值,而 col1s 和 col2s 是列表。当我编写 csv 文件时:

with open(ausgabe, "wb") as f:  
    datawriter = csv.writer(f)
    for each in zip(lonwgs84 , latwgs84, col1s, col2s):
        cols = each[0] , each[1], each[2] + each[3]
        datawriter.writerow(cols)

我得到以下输出:

51.821336803,11.6756790532,"['~11:16:05.833', '$GPGGA', '091607.00', '5149.28020818', 'N', '01140.54074319', 'E', '', '', '000.01']"

但我需要的是:

51.821336803,11.6756790532,~11:16:05.833,$GPGGA,091607.00,5149.28020818,N,01140.54074319,E,,,000.01

我尝试过拆分,但这似乎只适用于字符串。也许有人知道如何做到这一点?非常感谢!

4

1 回答 1

0

只需切片您的行以获得前 2 个元素,连接第 3 个和第 4 个元素:

datawriter.writerow(each[:2] + each[3] + each[4])

或者,首先解压缩元素:

for fp1, fp2, lst1, lst2 in zip(lonwgs84 , latwgs84, col1s, col2s):
    datawriter.writerow([fp1, fp2] + lst1 + lst2)

演示:

>>> [fp1, fp2] + lst1 + lst2
[51.821336803, 11.6756790532, '~11:16:05.833', '$GPGGA', '091607.00', '5149.28020818', 'N', '01140.54074319', 'E', '', '', '000.01']

您创建了前 2 个元素的元组,以及与连接的后两个元素+

cols = each[0], each[1], each[2] + each[3]

导致 的嵌套结构(each[0], each[1], [elements of each[2] and each[3]])

于 2013-07-24T08:42:46.687 回答