Merlin
Merlin

Reputation: 25659

How to convert all arguments to string in string formatting

Starting with this from Corey Goldberg:

#!/usr/bin/env python

import json
import pprint
import urllib2


def get_stock_quote(ticker_symbol):   
    url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
    lines = urllib2.urlopen(url).read().splitlines()
    return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))


if __name__ == '__main__':
    quote = get_stock_quote('IBM')
    print 'ticker: %s' % quote['t']
    print 'current price: %s' % quote['l_cur']
    print 'last trade: %s' % quote['lt']
    print 'full quote:'
    pprint.pprint(quote)

Using this:

import urllib2, json

def get_stock_quote(ticker_symbol):   
    url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
    lines = urllib2.urlopen(url).read().splitlines()
    #print lines
    return json.loads(''.join([x for x in lines if x not in ('// [', ']')]))



if __name__ == '__main__':
    symbols = ('Goog',) 
    symbols2 = ('Goog','MSFT')
    quote = get_stock_quote(symbols)
    print 'ticker: %s' % quote['t'],  'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['ltt']
    print quote['t'],  quote['l'], quote['ltt']

Usings symbols works, symbols2 does not work. The error message is

TypeError: not all arguments converted during string formatting

How do I convert all arguments to string in string formatting. In browser, the code that works is: Goog,MSFT.

EDIT: the output I am looking for is a list with goog, msft info.

Upvotes: 1

Views: 2073

Answers (4)

diegueus9
diegueus9

Reputation: 31572

In this case you can make:

import urllib2, json

def get_stock_quote(ticker_symbol):
    if isinstance(ticker_symbol, (list, tuple)):
        ticker_symbol = ','.join(ticker_symbol)
    url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol
    lines = urllib2.urlopen(url).read().splitlines()
    #print lines
    return json.loads('[%s]' % ''.join([x for x in lines if x not in ('// [', ']')]))

if __name__ == '__main__':
    symbols = ('Goog',) 
    symbols2 = ('Goog','MSFT')
    quotes = get_stock_quote(symbols2)
    for quote in quotes:
        print 'ticker: %s' % quote['t'],  'current price: %s' % quote['l_cur'], 'last trade: %s' % quote['ltt']
        print quote['t'],  quote['l'], quote['ltt']

Upvotes: 1

naeg
naeg

Reputation: 4002

Your problem is that %s can't handle a tuple with 2 elements, see that example below:

>>> "=%s" % ('Goog',)
'=Goog'
>>> "=%s" % ('Goog','MSFT')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

Anyway, use format() instead of %.

Upvotes: 2

NPE
NPE

Reputation: 500663

The problem is here:

url = 'http://finance.google.com/finance/info?q=%s' % ticker_symbol

With symbols2, you're supplying a 2-tuple whereas the formatting operator expects a scalar or a 1-tuple.

The following will fix the immediate error:

url = 'http://finance.google.com/finance/info?q=%s' % ",".join(ticker_symbol)

This only fixes half of the problem: the code that parses the results will also need to change. I leave this as an exercise for the reader.

Upvotes: 0

varela
varela

Reputation: 1331

You got this error because formatting require only one string, while you put tuple with 2 strings. If you want to get http://finance.google.com/finance/info?q=Goog,MSFT you should do

quote = get_stock_quote(",".join(['Goog','MSFT']))

Or do this in cycle:

for symbol in ('Goog', 'MSFT'):
    quote = get_stock_quote(symbol)

Upvotes: 0

Related Questions