Row count in a csv file

后端 未结 5 1575
情深已故
情深已故 2020-12-01 20:31

I am probably making a stupid mistake, but I can\'t find where it is. I want to count the number of lines in my csv file. I wrote this, and obviously isn\'t working: I have

5条回答
  •  旧巷少年郎
    2020-12-01 21:16

    First you have to open the file with open

    input_file = open("nameOfFile.csv","r+")
    

    Then use the csv.reader for open the csv

    reader_file = csv.reader(input_file)
    

    At the last, you can take the number of row with the instruction 'len'

    value = len(list(reader_file))
    

    The total code is this:

    input_file = open("nameOfFile.csv","r+")
    reader_file = csv.reader(input_file)
    value = len(list(reader_file))
    

    Remember that if you want to reuse the csv file, you have to make a input_file.fseek(0), because when you use a list for the reader_file, it reads all file, and the pointer in the file change its position

提交回复
热议问题