Find number of columns in csv file

后端 未结 5 982
无人及你
无人及你 2021-02-02 08:53

My program needs to read csv files which may have 1,2 or 3 columns, and it needs to modify its behaviour accordingly. Is there a simple way to check the number of columns withou

5条回答
  •  生来不讨喜
    2021-02-02 09:18

    I would suggest a simple way like this:

    with open('./testfile.csv', 'r') as csv:
         first_line = csv.readline()
         your_data = csv.readlines()
    
    ncol = first_line.count(',') + 1 
    

提交回复
热议问题