On Thu, Jul 30, 2009 at 4:26 PM, Marv Boyes<[email protected]> wrote: > I'm very sorry; I should have been more explicit in what it is I'm working > with. > > The response from the server consists of a pair of hashes and a list of URLs > for doing different things with the file the hashes represent. So the full > response is like this: > > file_hash > delete_hash > http://server.com/file_hash.ext > http://server.com/file_hashA.ext > http://server.com/file_hashB.ext > http://server.com/file_hashC.ext > http://server.com/delete/deletehash > > I'm hoping to assign each line of that response to a separate variable so I > can format the output on a case-by-case basis, e.g.: > > direct_link = <third URL in response> > print "Direct link to file: %s' % direct_link > > -or- > > delete_file = <seventh URL in response> > print "Delete the file: %s' % delete_file > > I've got seven lines worth of server response, their order is significant, > and I need to be able to present each value in an arbitrary way. I won't > necessarily be presenting these values to the user in the same order they > come in the server response. Some of the values I'll need to use elsewhere > in the script to do other things, but it won't be necessary to present those > values to the user. > > I'm not sure I'm even making sense to myself. > > > Kent Johnson wrote: >> >> On Thu, Jul 30, 2009 at 3:19 PM, Marv Boyes<[email protected]> 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 >> > > _______________________________________________ > Tutor maillist - [email protected] > http://mail.python.org/mailman/listinfo/tutor >
Hi Marv, While I'm not convinced hard-coding variables to list items is the best solution here, you could achieve it with tuple unpacking: urllist = urls.split() # urllist should have three items in it, otherwise an exception is raised. # You will need to adapt this to the length of your list of urls. link_one,link_two,link_three = urllist print "the first link:", link_one print "the second link:", link_two Best, Jeremy _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
