Read specific columns from a csv file with csv module?

后端 未结 12 1155
闹比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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 10:47

    To fetch column name, instead of using readlines() better use readline() to avoid loop & reading the complete file & storing it in the array.

    with open(csv_file, 'rb') as csvfile:
    
        # get number of columns
    
        line = csvfile.readline()
    
        first_item = line.split(',')
    

提交回复
热议问题