I try to edit and find data from a text file with python 2.7 (data from stock market)

Peter Otten __peter__ at web.de
Fri Sep 7 12:25:33 EDT 2018


alon.najman at gmail.com wrote:

> hi,
> 
> I try to edit a text file with python 2.7:
> 
> ***** AAPL *****
> Date: September 07 2018
> Time: 14:10:52
> Price: ,068,407
> Ask: None
> High: None
> Low: None
> Previous Close: ,068,407
> Volume: $ 227.35 / $ 221.30
> Market Cap: 20.23

It looks like the author of the nasdaq_stock module was interested in a 
quick and dirty tool for personal needs rather than a clean library for 
general use.

The text quoted above is printed implicitly by the stock() function. To 
suppress it you have to modify that function or to redirect sys.stdout.

Another problem with the code is that it may swallow arbitrary exceptions. 
Therefore my error message below has to be vague.

> but when I write it to a file I get:
> {'previous_close': ',068,407', 'volume':
> {u'$\xa0227.35\xa0/\xa0$\xa0221.30', 'market_cap': '20.23', 'price':
> {',068,407', 'high': 'None', 'ask': 'None', 'low': 'None', 'time':
> {'14:15:45', 'date': 'September 07 2018', 'ticker': 'AAPL'}
> 
> why is that? 

If all goes well the stock function returns the above dict.

> and how do I get the price value only? so I will have only
> that in a variable? for example the variable: Price.

import sys
from nasdaq_stock import nasdaq_stock
 
stock_info = nasdaq_stock.stock('AAPL')

print
if stock_info:
    price = stock_info["price"]
    ticker = stock_info["ticker"]
    print "Ticker:", ticker, "Price:", price
else:
    print >> sys.stderr, "Something went wrong"





More information about the Python-list mailing list