Python Forum
Pulling & Reading Date from UDF that Compare it to Live Data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pulling & Reading Date from UDF that Compare it to Live Data
#5
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 row
Have 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_stock
To do a test made apple.csv:
Output:
header 2,Name,a,1 1,John,b,204.4444 2,Eric,c,200.2899932861328 3,Brad,d,177.9499969482422
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}')

Output:
1 200.2899932861328 177.9499969482422 ---------- Live price now is: 202.58999633789062 count below from apple.csv is: 3
Reply


Messages In This Thread
RE: Pulling & Reading Date from UDF that Compare it to Live Data - by snippsat - Jul-20-2019, 09:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 261 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 634 Jan-20-2024, 04:45 AM
Last Post: 1418
  Pulling Specifics Words/Numbers from String bigpapa 2 784 May-01-2023, 07:22 PM
Last Post: bigpapa
  Having trouble installing scikit-learn via VSC and pulling my hair out pythonturtle 1 775 Feb-07-2023, 02:23 AM
Last Post: Larz60+
  Create simple live plot of stock data dram 2 2,943 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,251 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,388 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Pulling username from Tuple pajd 21 3,452 Oct-07-2022, 01:33 PM
Last Post: pajd
  Reading Data from JSON tpolim008 2 1,107 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  How can I compare 2 format of date? korenron 4 1,545 Dec-21-2021, 12:40 PM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020