[Tutor] Re: Convert list to literal string.

Decibels decibelshelp@charter.net
Sat Jun 28 19:48:02 2003


Got it thanks! See below.

> Abel Daniel
> >Decibels wrote:
> > I can make a loop and process the info. But you can pass Yahoo
> > an entire list of symbols and it will return the information.
> > I can make this work:
> >
> > def addstock_info(addstock):
> >         stock = addstock
> >         scount = len(stock)
> >         print "Getting Stockinfo data from Yahoo"
> >         for x in range(0,scount):
> >                 results=quotes.findQuotes(stock[x])
>
> I think this would be better written as:
>
> def addstock_info(addstock):
>         print "Getting Stockinfo data from Yahoo"
>         for i in addstock:
>                 results=quotes.findQuotes(i)
>                 # Of course, we are throwing away all but the last
>                 # result, but thats what the original code did, too

Yes, would be better. There is a another part below, so the 
results are processed to populate the database. So it isn't lost.
Was a sloppy in the code guess. Had a lot of print statements
and commented out tries to see why it wasn't working.  
If could make the command-line work the same as the literal string
then wouldn't need this loop.

> I think this is what you need:
> >>> l=['IBM','EK','MO','UTX']
> >>> l
>
> ['IBM', 'EK', 'MO', 'UTX']
>
> >>> s=','.join(l)
> >>> s
>
> 'IBM,EK,MO,UTX'
>
> and pass s to quotes.findQuotes

Hmmm, WOW. That works!!!!!
I am still not sure why?!? I never thought of using join, since already had ',' from the command-line.
So I used code you suggested above for user input of symbols wanting, then 

stock = ",".join(addstock)  #convert user input to literal string. (not sure if correct terminology, but does same thing.)

and processed it and populated the database.

Thank you,

Dave