Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change output of cells.
#1
Dear users,

I am quite new to Python coding, and recently I have found a way to download stock data into Python.
Since I am downloading multiple stocks at once, I want to change the output of what Python gives me as a return of this string.

The string I am using is:

import requests

url = "https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7"

response = requests.post(url)
print (response.text)
If i copy this string multiple times and change the ticker symbol, i will receive data from multiple stocks.

The output i Receive is
Output:
Date,Open,High,Low,Close,Adj Close,Volume 2018-01-02,170.160004,172.300003,169.259995,172.259995,169.712067,25555900 2018-01-03,172.529999,174.550003,171.960007,172.229996,169.682510,29517900 2018-01-04,172.539993,173.470001,172.080002,173.029999,170.470703,22434600 2018-01-05,173.440002,175.369995,173.050003,175.000000,172.411560,23660000
(I get more rows, but for argument sake this is enough)


I would like the output to be changed in a way that infront of every row, it will say the ticker simbol. (which in this case is AAPL)

So an example would be:
Output:
Ticker,Date,Open,High,Low,Close,Adj Close,Volume AAPL,2018-01-02,170.160004,172.300003,169.259995,172.259995,169.712067,25555900
Is there anyone who can help me with this issue?

Kind regards,



Hoogoo
Reply
#2
Thank you Larz60+, i will do that the next time!

Still. Anyone knows a fix for my issue?
Reply
#3
This code requires python 3.6 or newer

You should write something reusable and better formatted. I set the variables up as arguments
to the quote routine. You can add inputs do that they can be varied:
import requests
import sys


def get_quote(ticker, period1, period2, events, interval, crumb):
    url = f"{'https://query1.finance.yahoo.com/v7/finance/download/'}{ticker}" \
        f"?period1={period1}" \
        f"&period2={period2}" \
        f"&interval={interval}" \
        f"&events={events}" \
        f"&crumb={crumb}"
    print(f'\n{url}')
    # url = "https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7"
    #        https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7
    response = requests.post(url)
    return response.text

def get_quotes():
    while True:
        ticker = input('Enter ticker symbol or quit: ')
        if ticker == 'quit':
            break
        period1 = '1514761200'
        period2 = '1517439600'
        events = 'history'
        interval = '1d'
        crumb = 'Nr.jC4aJCc7'
        results = get_quote(ticker, period1, period2, events, interval, crumb)
        print(f'\n============================================================================')
        print(f'Quotes for {ticker}')
        print(f'============================================================================')
        header = f"{'Date':14} {'Open':14} {'High':14} {'Low':14} {'Close':14} {'Volume':14}\n"
        results = results.split('\n')
        for entry in results:
            entry = entry.strip().split(',')
            for item in entry:
                print(f'{item:14}', end='')
            print()

if __name__ == '__main__':
    get_quotes()
test it:
Output:
Enter ticker symbol or quit: AAPL https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7 ============================================================================ Quotes for AAPL ============================================================================ Date Open High Low Close Adj Close Volume 2018-01-02 170.160004 172.300003 169.259995 172.259995 169.712067 25555900 2018-01-03 172.529999 174.550003 171.960007 172.229996 169.682510 29517900 2018-01-04 172.539993 173.470001 172.080002 173.029999 170.470703 22434600 2018-01-05 173.440002 175.369995 173.050003 175.000000 172.411560 23660000 2018-01-08 174.350006 175.610001 173.929993 174.350006 171.771179 20567800 2018-01-09 174.550003 175.059998 173.410004 174.330002 171.751465 21584000 2018-01-10 173.160004 174.300003 173.000000 174.289993 171.712051 23959900 2018-01-11 174.589996 175.490005 174.490005 175.279999 172.687408 18667700 2018-01-12 176.179993 177.360001 175.649994 177.089996 174.470642 25226000 2018-01-16 177.899994 179.389999 176.139999 176.190002 173.583969 29565900 2018-01-17 176.149994 179.250000 175.070007 179.100006 176.450928 34386800 2018-01-18 179.369995 180.100006 178.250000 179.259995 176.608551 31193400 2018-01-19 178.610001 179.580002 177.410004 178.460007 175.820389 32425100 2018-01-22 177.300003 177.779999 176.600006 177.000000 174.381973 27108600 2018-01-23 177.300003 179.440002 176.820007 177.039993 174.421387 32689100 2018-01-24 177.250000 177.300003 173.199997 174.220001 171.643082 51105100 2018-01-25 174.509995 174.949997 170.529999 171.110001 168.579086 41529000 2018-01-26 172.000000 172.000000 170.059998 171.509995 168.973175 39143000 2018-01-29 170.160004 170.160004 167.070007 167.960007 165.475677 50640400 2018-01-30 165.529999 167.369995 164.699997 166.970001 164.500336 46048200 2018-01-31 166.869995 168.440002 166.500000 167.429993 164.953522 32478900 Enter ticker symbol or quit: LCI https://query1.finance.yahoo.com/v7/finance/download/LCI?period1=1514761200&period2=1517439600&interval=1d&events=history&crumb=Nr.jC4aJCc7 ============================================================================ Quotes for LCI ============================================================================ Date Open High Low Close Adj Close Volume 2018-01-02 23.200001 24.650000 22.700001 23.650000 23.650000 1101300 2018-01-03 23.549999 24.750000 23.400000 24.400000 24.400000 639500 2018-01-04 24.500000 24.500000 23.700001 24.250000 24.250000 402800 2018-01-05 24.200001 24.250000 23.700001 24.049999 24.049999 400800 2018-01-08 24.049999 24.174999 23.299999 23.549999 23.549999 446700 2018-01-09 23.700001 25.100000 23.700001 24.950001 24.950001 1034000 2018-01-10 24.799999 25.350000 24.450001 24.850000 24.850000 670100 2018-01-11 24.799999 25.400000 24.750000 24.850000 24.850000 527100 2018-01-12 24.900000 25.299999 24.650000 24.750000 24.750000 452000 2018-01-16 24.750000 24.799999 24.299999 24.400000 24.400000 386700 2018-01-17 24.400000 25.025000 24.350000 24.750000 24.750000 462800 2018-01-18 24.799999 24.799999 22.150000 23.650000 23.650000 1022700 2018-01-19 23.900000 24.750000 22.950001 23.000000 23.000000 787100 2018-01-22 23.000000 23.400000 22.700001 23.049999 23.049999 911100 2018-01-23 23.100000 23.500000 22.799999 23.200001 23.200001 312100 2018-01-24 23.100000 23.250000 22.450001 22.950001 22.950001 692300 2018-01-25 23.000000 23.100000 22.450001 22.950001 22.950001 448900 2018-01-26 23.049999 23.250000 22.549999 23.200001 23.200001 411200 2018-01-29 23.150000 23.400000 22.350000 22.600000 22.600000 560200 2018-01-30 22.250000 22.700001 21.500000 21.650000 21.650000 1055300 2018-01-31 21.850000 22.350000 20.200001 20.350000 20.350000 644800 Enter ticker symbol or quit: quit
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output values change akbarza 3 534 Oct-18-2023, 12:30 PM
Last Post: deanhystad
  Find a specific keyword after another keyword and change the output sgtmcc 5 849 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Rain sensor output only on change Pete6 3 2,048 May-11-2022, 10:36 PM
Last Post: Pete6
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,865 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  How can I iterate through all cells in a column (with merge cells) with openpyxl? aquerci 1 7,531 Feb-11-2021, 09:31 PM
Last Post: nilamo
  Copy certain cells into new workbook certain cells Kristenl2784 4 2,503 Jul-14-2020, 07:59 PM
Last Post: Kristenl2784
  Change Text Color Output in Python bluethundr 2 8,744 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,840 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Change the output window size and display area issac_n 0 2,275 Apr-13-2018, 04:41 AM
Last Post: issac_n
  Change Windows Sound Output Device with Python delfar 1 10,333 Sep-15-2017, 12:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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