你能把dict()转换成序列吗?

时间:2016-02-25 00:30:32

标签: csv dictionary python-3.4

我对csv中找到的所有nmea句子都有一个dict()。我尝试创建另一个csv来将dict()的结果写入其中以用于统计和日志记录。但是,我不能因为dict()不能被cal'?

import csv

#Counts the number of times a GPS command is observed
def list_gps_commands(data):
    """Counts the number of times a GPS command is observed.

Returns a dictionary object."""

    gps_cmds = dict()
    for row in data:
        try:
            gps_cmds[row[0]] += 1 
        except KeyError:
            gps_cmds[row[0]] = 1

    return gps_cmds

print(list_gps_commands(read_it))
print ("- - - - - - - - - - - - -")


with open('gpsresults.csv', 'w') as csvfile:
  spamwriter = csv.writer(csvfile, delimiter=',', dialect='excel')
  spamwriter.writerow(list_gps_commands(read_it))

有人能帮助我吗?有没有办法可以将键/值转换为序列,以便csv模块可以识别它?或者另一种方式?

1 个答案:

答案 0 :(得分:1)

使用companies代替csv.DictWriter