dimanche 29 janvier 2017

If statement string comparison in python

I want to start by saying that I'm a complete beginner in Python.

What I'm trying to do is read data from a relatively big CSV file (about 33k data points). The data consist of various readings each five minutes of 3 different stations from October until today. What I want to do is filter the data so I only get the results from 5 pm (17:00) to 7 pm (19:00) (of every day) and analyze only that set of data.

The code I have so far is this:

import csv

#Initialize list of different columns from my CSV file
date = []
time = []
Value1 = []
Value2 = []
Value3 = []

with open('MW_DATA_DIV.csv' , 'r') as csvfile:
    test = csv.reader(csvfile, delimiter= ',')
    for row in test:
        date.append(row[0])
        time.append(row[1])
        Value1.append((row[2]))
        Value2.append((row[3]))
        Value3.append((row[4]))


#print to see if all the data was acquired as desired
'''
print (len(time))
print (len(Value1))
print (len(Value2))
print (len(Value3))
print (len(date))
'''

#Sets a counter to run 1 by 1 thru the list "time"

for counter in time:
  hours = counter[0:2]   #Get first 2 digits (i.e. 17, which are the hours i want to take data from)
  #print (hours)
  #print (type(hours))

  if hours is "15" :
    print("[add code HERE]")
  elif hours is "16" :
    print("[also add code hereee]")
  elif hours is "17":
      print("Also Add code HERE")

The problem I'm having is that I'm not getting any message (add code here) So it's like its not executing the if statements. I have already checked and the data is a string. and whenever I print(hours) I get the readings of every hour (i.e: 0:05 , 0:10 , 0:15 , ... , 23:55 , ... , etc) just like I want...

I'm not sure what problem I have in my if statements that it's not executing them. Any help/advice/QUESTIONS are welcomed!!

Aucun commentaire:

Enregistrer un commentaire