[Tutor] Assigning each line of text to a separate variable

vince spicer vinces1979 at gmail.com
Thu Jul 30 21:28:07 CEST 2009


On Thu, Jul 30, 2009 at 1: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
>
> And so on. I know this should be extremely easy, but I appear to be having
> some manner of mental block. Any and all guidance would be greatly
> appreciated; many thanks in advance.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Hello,

you could use a dictionary to assign a key value and a descrption, but
without seeing some data this is very rough

data = {}
for line in response:
    key, value = response.split(",")  # line is "myname, http://blah.com"
    data[key] = value

#then later
for key, value in data.items():
    print "Link To %s : %s" % key, value
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090730/c88bebfe/attachment-0001.htm>


More information about the Tutor mailing list