J.Greenslade
J.Greenslade

Reputation: 367

Get number of rows from .csv file

I am writing a Python module where I read a .csv file with 2 columns and a random amount of rows. I then go through these rows until column 1 > x. At this point I need the data from the current row and the previous row to do some calculations.

Currently, I am using 'for i in range(rows)' but each csv file will have a different amount of rows so this wont work.

The code can be seen below:

rows = 73
    for i in range(rows):  

        c_level = Strapping_Table[Tank_Number][i,0]  # Current level
        c_volume = Strapping_Table[Tank_Number][i,1] # Current volume

        if c_level > level:

            p_level = Strapping_Table[Tank_Number][i-1,0]    # Previous level
            p_volume = Strapping_Table[Tank_Number][i-1,1]  # Previous volume

            x = level - p_level   # Intermediate values
            if x < 0:
                x = 0
            y = c_level - p_level
            z = c_volume - p_volume

            volume = p_volume + ((x / y) * z)

            return volume

When playing around with arrays, I used:

for row in Tank_data: 
   print row[c]       # print column c
   time.sleep(1) 

This goes through all the rows, but I cannot access the previous rows data with this method.

I have thought about storing previous row and current row in every loop, but before I do this I was wondering if there is a simple way to get the amount of rows in a csv.

Upvotes: 1

Views: 1044

Answers (3)

taoufik A
taoufik A

Reputation: 1410

Store the previous line

with open("myfile.txt", "r") as file:
     previous_line = next(file)
     for line in file:
        print(previous_line, line)
        previous_line = line

Or you can use it with generators

def prev_curr(file_name):
    with open(file_name, "r") as file:
        previous_line = next(file)
        for line in file:
            yield previous_line ,line
            previous_line = line
# usage
for prev, curr in prev_curr("myfile"):
    do_your_thing()

Upvotes: 1

Fred Cascarini
Fred Cascarini

Reputation: 62

Since the size of each row in the csv is unknown until it's read, you'll have to do an intial pass through if you want to find the number of rows, e.g.:

numberOfRows = (1 for row in file)

However that would mean your code will read the csv twice, which if it's very big you may not want to do - the simple option of storing the previous row into a global variable each iteration may be the best option in that case.

An alternate route could be to just read in the file and analyse it from that from e.g. a panda DataFrame (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) but again this could lead to slowness if your csv is too big.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599610

You should use enumerate.

for i, row in enumerate(tank_data): 
   print row[c], tank_data[i-1][c]

Upvotes: 0

Related Questions