Read specific columns from a csv file with csv module?

后端 未结 12 1074
闹比i
闹比i 2020-11-22 10:06

I\'m trying to parse through a csv file and extract the data from only specific columns.

Example csv:

ID | N         


        
12条回答
  •  旧时难觅i
    2020-11-22 10:44

    If you need to process the columns separately, I like to destructure the columns with the zip(*iterable) pattern (effectively "unzip"). So for your example:

    ids, names, zips, phones = zip(*(
      (row[1], row[2], row[6], row[7])
      for row in reader
    ))
    

提交回复
热议问题