[Tutor] Assigning each line of text to a separate variable
Kent Johnson
kent37 at tds.net
Thu Jul 30 21:39:47 CEST 2009
On Thu, Jul 30, 2009 at 3:19 PM, Marv Boyes<marvboyes at gmail.com> wrote:
> Hello, all. This is probably embarrassingly basic, but I haven't been able
> to find something that works.
>
> I'm working on a script that needs to manipulate a list (not 'list' in the
> Python sense) of URLs returned in a server response. Right now, I'm
> stripping the XML tags from that response and assigning the resulting list
> of URLs to a variable so I can print it in the terminal. So when I do, say,
> 'print urls' I get something like this:
>
> http://server.com/thing1
> http://server.com/thing2
> http://server.com/thing3
>
> And so on. What I would _like_ to do is assign each line of that list to a
> separate variable, so that I can format my output to be more explicit;
> something like this:
>
> Link to Thing1: http://server.com/thing1
> Link to Thing2: http://server.com/thing2
It looks like your "list" of URLs is a string containing one URL per
line. If you put it in an actual list, you can process it more
flexibly. Something like
urlList = urls.splitlines()
for i, url in enumerate(urlList):
print "Link to Thing%s: %s" % (i, url)
Kent
More information about the Tutor
mailing list