Pulling & Reading Date from UDF that Compare it to Live Data - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Pulling & Reading Date from UDF that Compare it to Live Data (/thread-19912.html) |
Pulling & Reading Date from UDF that Compare it to Live Data - firebird - Jul-19-2019 Hello Everyone! Would you help me solve the following problem i have: I am trying to write a user defined function that would load, read date from a csv file, itereate through the column where row of values being examined are located, check those values against current value (price = live.get_live_price("AAPL"))and then take an action (in this case to print it out) if the comparison meet certain condition (row value < price or row == price). when i run the script i do not get any error message and yet there is no output at all. Thank you #import all necessary modules import csv from yahoo_fin import stock_info as live import pandas as pd from datetime import date import datetime def readfile(filepath): #get Apple's latest quote price price = live.get_live_price("AAPL") with open(r'C:\Users\...\Desktop\AAPLCSV', 'r') as file: reader = csv.reader(file) for index, row in reader: if row[3] < price: return price elif row[3] == price: return row[3] print(price, row[3]) RE: Pulling & Reading Date from UDF that Compare it to Live Data - snippsat - Jul-19-2019 (Jul-19-2019, 07:50 PM)firebird Wrote: when i run the script i do not get any error message and yet there is no output at all.Because you don't call the function( readfile ) at all.There are both ValueError and logical error in your code Some points: Can not use this without enumerate() # for index, row in reader: for index, row in enumerate(reader):price is float then need to compare with float,when read a csv you always get string out. >>> from yahoo_fin import stock_info as live >>> >>> price = live.get_live_price("AAPL") >>> price 202.58999633789062 >>> type(price) <class 'numpy.float64'>When do like this with eg float,it will just return(out) as soon as True. Then this do not make sense as don't know if 1 or 10 value it return price of. if float(row[3]) < price: return priceRemove function and start testing small scale with hint given. RE: Pulling & Reading Date from UDF that Compare it to Live Data - firebird - Jul-20-2019 Hi snippsat, Thank you for having taken the time to review and reply and made some valuable suggestion for assisting me. Approaching to solve it 'small scale', the reworked code gets me the following output (below). Pardon my ignorance, when it comes to calling the function, i then have to create another function, don't i? Output: Apple's closing price today is 202.58999633789062 Values in row[3] from AAPLcsv file 177.9499969482422 176.22999572753906 175.60000610351562 175.44000244140625 184.27999877929688 183.0800018310547 186.50999450683594 191.80999755859375 194.86000061035156 193.9499969482422 194.6999969482422 191.5500030517578 192.89999389648438 196.0500030517578 199.67999267578125 200.3699951171875 198.8000030517578 198.5399932861328 198.42999267578125 197.77000427246094 200.2899932861328 198.67999267578125 Reworked code: #import all necessary modules import csv from yahoo_fin import stock_info as live import pandas as pd from datetime import date import datetime #get Apple's latest quote price price = live.get_live_price("AAPL") with open(r'C:\Users\...\Desktop\AAPLcsv.csv', 'r') as file: reader = csv.reader(file) next(reader, None)#skip the header print("Apple's closing price today is", price) print('Values in row[3] from AAPLcsv file') for index, row in enumerate(reader): if float(row[3]) < price: print(row[3]) RE: Pulling & Reading Date from UDF that Compare it to Live Data - firebird - Jul-20-2019 Hello, I'm still in need of some help with this code snippet. I revised my earlier code and now managing to call my function 'readfile' successfully. However, i'm stuck with this error message which generated in the part of 'if __name__ == "__main__":'. I can't seem to pass the header of column in index 3 (row[3])- which is a string. The error i get: This is my new, reworked code:#import all necessary modules import csv from yahoo_fin import stock_info as live import pandas as pd from datetime import date import datetime def readfile(filepath): with open(r'C:\Users\...\Desktop\AAPLcsv.csv', 'r') as file: reader = csv.reader(file) next(reader, None)#skip header for index, row in enumerate(reader): return row if __name__ == "__main__": #get Apple's latest quote price path = r'C:\Users\...\Desktop\AAPLcsv.csv' price = live.get_live_price("AAPL") for index, row in enumerate(readfile(path)): if float(row[3]) < price: print(row[3]) RE: Pulling & Reading Date from UDF that Compare it to Live Data - snippsat - Jul-20-2019 This will just iterate over all rows,and return only last row. def readfile(filepath): with open(r'C:\Users\...\Desktop\AAPLcsv.csv', 'r') as file: reader = csv.reader(file) next(reader, None)#skip header for index, row in enumerate(reader): return rowHave to collect all row in list before return. def readfile(filepath=''): apple_stock = [] with open('apple.csv', 'r') as file: reader = csv.reader(file) next(reader, None)#skip header for index, row in enumerate(reader): apple_stock.append(row) return apple_stockTo do a test made apple.csv: Added count and show live price.#import all necessary modules import csv from yahoo_fin import stock_info as live import pandas as pd from datetime import date import datetime def readfile(filepath=''): apple_stock = [] with open('apple.csv', 'r') as file: reader = csv.reader(file) next(reader, None)#skip header for index, row in enumerate(reader): apple_stock.append(row) return apple_stock if __name__ == "__main__": #get Apple's latest quote price path = r'apple.csv' price = live.get_live_price("AAPL") count = 0 for index, row in enumerate(readfile(path)): if float(row[3]) < price: print(row[3]) count += 1 print('-' * 10) print(f'Live price now is: {price} count below from {path} is: {count}')
|