How to know accounts lines have a csv file?

1

I want to know stories lines I have in a csv file with a tab delimiter. I try the following in a file countRowsCSV.py :

import csv

with open('archivo.csv',"r") as f:
    reader = csv.reader(f,delimiter = "\t")
    data = list(reader)
    row_count = len(data)

    print row_count

But I answered the terminal:

$ python countRowsCSV.py 
Traceback (most recent call last):
  File "countRowsCSV.py", line 7, in <module>
    data = list(reader)
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
    
asked by ThePassenger 02.12.2017 в 20:58
source

1 answer

1

The error in principle is generated by trying to read a file that uses a different end to the one used by your OS (LF ( \n ) in Linux) as CR + LF ( \r\n ) or CR ( \r ) or some inconsistency in the line ends. It is common for this to occur with files created with Excel that contain, for example, empty cells.

Try to open the document in universal-newline mode as the exception itself suggests, for this :

with open('archivo.csv',"rU") as f:

If you only want to obtain the number of lines and you do not need the content I suggest you use sum with a generator:

with open('archivo.csv',"rU") as f:
    row_count = sum(1 for row in f)
    
answered by 02.12.2017 / 21:29
source