Write a partial row with csv.DictWriter?

后端 未结 1 432
温柔的废话
温柔的废话 2021-01-24 13:58

I have a CSV file with a group of inputs.

Example:

A B C D

I want to analyze the results and output a CSV file for each row such as:

1条回答
  •  伪装坚强ぢ
    2021-01-24 14:23

    Yes, there's a simple way to do it. When you create the csv.DictWriter instance being used, just pass it the keyword argument extrasaction='ignore'—the default value for it is 'raise', which is the cause of the ValueError exception you're getting when you pass it a dictionary with keys not specified in the fieldnames parameter.

    BTW, reading the relevant documentation before asking SO questions is often a worthwhile endeavor—and can often provide answers without further delay.

    0 讨论(0)
提交回复
热议问题