因为您有一个字典,其中单词是键,它们的频率是值,所以DictWriter不适合。对于共享一些公共键集的映射序列来说,这是很好的,它们用作csv的列。例如,如果您有诸如手动创建的dict列表:a_list = [{'dictionary[word]': '1', 'word': 'inherited'},
{'dictionary[word]': '1', 'word': 'inheritance'},
{'dictionary[word]': '1', 'word': 'inherit'}]
那么DictWriter将是该作业的工具。但是你有一个单一的dictionary,比如:
^{pr2}$
但是,您已经将(freq, word)对的排序列表构建为aux,非常适合写入csv:with open('Aeschylus.csv', 'wb') as csvfile:
header = ['frequency', 'word']
writer = csv.writer(csvfile)
writer.writerow(header)
# Note the plural method name
writer.writerows(aux)python seems to be totally ignoring the csv part and just printing the data as if I didn't code for the csv.
听起来很奇怪。至少你应该