Sara M.
Sara M.

Reputation: 13

.seek does not work for resetting csv.reader containing strings too, what other method could be used?

I am trying to reset the csv.reader but the solution posted by others does not work of course because of the presence of strings...what other method could be used instead of csv_reader.seek(0) to reset the csv.reader? I am new to this so I would much appreciate any insights...

I also tried turning the column into a list as well which causes additional errors.

import os
import csv

csv_path=os.path.join("..","Resources","budget_data_1.csv")
with open(csv_path,newline="") as csvfile:
    csv_reader=csv.reader(csvfile,delimiter=",")

    next(csv_reader)


    count=0
    for i in csv_reader:
        count+=1
    print(count)

    csv_reader.seek(0)

    total=0
    for row in csv_reader:
        total+=int(row[1])
    print(total)

Upvotes: 1

Views: 737

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

you cannot seek in a csv.reader object, but you can seek on the underlying handle and skip the title line again...:

csvfile.seek(0)    
next(csv_reader)  # skip title line ... again

note that since you're reading 2 times, the best way would be to store the rows in a list:

csv_reader = list(csv_reader)

now your code becomes (with a comprehension passed to sum to make it more pythonic):

with open(csv_path,newline="") as csvfile:
    csv_reader=csv.reader(csvfile,delimiter=",")

    next(csv_reader)               # skip title line
    csv_reader = list(csv_reader)  # turn the csv into a list
    count = len(csv_reader)        # lists have a "len" method
    print(count)

    # compute total by summing every second col of each row
    total = sum(int(row[1]) for row in csv_reader)
    print(total)

if you only need the 2nd column to compute the mean, don't store the file fully, just data from 2nd column:

    next(csv_reader)               # skip title line
    col2 = [int(row[1]) for row in csv_reader]  # create the list of integers
    mean = sum(col2)/len(col2)  # division may differ depending on python version

Upvotes: 1

Related Questions