1

Read CSV get the number of lines AND the first two lines of the CSV

 2 years ago
source link: https://www.codesd.com/item/read-csv-get-the-number-of-lines-and-the-first-two-lines-of-the-csv.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Read CSV get the number of lines AND the first two lines of the CSV

advertisements

I'm assuming I'm getting this error because I'm reading the same CSV twice (I don't fully understand why this is an issue tho) but I need the code below to do the following 2 things...

1) Give me a the total count of all the rows in the CSV.

2) Give me the first two lines only of the data for me to display back to the user.

file = upload.filepath
#I READ THE FILE
file_read = csv.reader(file)

#GET THE COUNT I.E. 100 ROWS
row_count = sum(1 for row in file_read)

#ADD TO DATA JUST THE FIRST TWO ROWS THAT I WILL USE TO DISPLAY BACK TO THE USER
data = []
for i in range(2):
    data.append(file_read.next())

How can I achieve this?


Why not reverse the two statements? You can read the first two lines in using your:

#ADD TO DATA JUST THE FIRST TWO ROWS THAT I WILL USE TO DISPLAY BACK TO THE USER
data = []
for i in range(2):
    data.append(file_read.next())

Then do the row count for the remaining rows, and add 2:

#GET THE COUNT I.E. 100 ROWS
row_count = 2 + sum(1 for row in file_read)

However, will give incorrect results if you have <2 rows. A better solution may be to simply read in the whole file, then count the rows in the resulting list.

import csv

# Open the file upload.filepath, get a pointer in file
# file automatically closed at end of 'with' context
with open(upload.filepath, 'r') as file:

    #I READ THE FILE
    file_read = csv.reader(file)

    data = []
    for row in file_read:
        data.append(row)

    rows = len(data)
    first_two_rows = data[:2]

If it's a massive file, and your worried about memory, you can add a count variable, and only append to data when its <=2

data = []
rows = 0
for row in file_read:
    rows += 1
    if rows<3:
        data.append(row)

Tags python

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK