Reading column names alone in a csv file

前端 未结 9 2316
不思量自难忘°
不思量自难忘° 2020-12-23 18:58

I have a csv file with the following columns:

id,name,age,sex

Followed by a lot of values for the above columns. I am trying to read the column names alone and

9条回答
  •  抹茶落季
    2020-12-23 19:27

    I literally just wanted the first row of my data which are the headers I need and didn't want to iterate over all my data to get them, so I just did this:

    with open(data, 'r', newline='') as csvfile:
    t = 0
    for i in csv.reader(csvfile, delimiter=',', quotechar='|'):
        if t > 0:
            break
        else:
            dbh = i
            t += 1
    

提交回复
热议问题