Count how many records are in a CSV Python?

后端 未结 16 1519
无人共我
无人共我 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

    This works for csv and all files containing strings in Unix-based OSes:

    import os
    
    numOfLines = int(os.popen('wc -l < file.csv').read()[:-1])
    

    In case the csv file contains a fields row you can deduct one from numOfLines above:

    numOfLines = numOfLines - 1
    

提交回复
热议问题