samedi 25 avril 2020

Python CSV reader not recognizing if statement conditions as true

I just started coding in Python and am trying to figure out what I'm doing wrong here:

I'm writing a test program to read a CSV file and, if a row fulfills two conditions (that row1 == 74 and row[5] == 1, to print that row's row[6] into another file.

I haven't gotten to the csv writing yet, as no matter what I modify the program isn't ever seeing my conditions as correct, even though there is one row which fulfills the conditions. I've tried setting the 74 and 1 values as integers as well (in the if statement code), and removing the 0 from 01, but nothing works.

Here's the code:

import csv

with open("march11constraints.csv", newline='') as myFile:
    reader = csv.reader(myFile)
    for row in reader:
            if (row[1] == "74" and row[5] == "01"):
                with open('shadowprice.csv','a') as doc:
                    v = str(row[6]).replace("'","")
                    y = v.replace("[","")
                    z = y.replace("]","")
                    doc.write(z)
                    print("Success")
            else: 
                print("Failure")

I would attach the csv file but not sure if that's possible -- here's the row in question that should be triggering the conditions: csv row

I hope I'm being clear enough, thanks.

Aucun commentaire:

Enregistrer un commentaire