我对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模块可以识别它?或者另一种方式?
答案 0 :(得分:1)
使用companies
代替csv.DictWriter
。