Count how many records are in a CSV Python?

后端 未结 16 1423
无人共我
无人共我 2020-11-29 16:43

I\'m using python (Django Framework) to read a CSV file. I pull just 2 lines out of this CSV as you can see. What I have been trying to do is store in a variable the total n

16条回答
  •  抹茶落季
    2020-11-29 17:28

    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

提交回复
热议问题