Le Tue, 26 May 2009 15:57:10 +0100, Dayo Adewunmi <contactd...@gmail.com> s'exprima ainsi:
> Hi, > > I'm extracting data from OpenLDAP, which needs to be formatted into > hyperlinks. So far, I can print the generated HTML code: > > print "<a href=\"http://users.example.com/~" + userName + ">" + lastName > + ", " + firstName + "</a>" This is the string object you'll have to save below. You'd better use "interpolated string" for legibility: html_link = "<a href=\"http://users.example.com/~%s>%s, %s</a>" %(userName,lastName,firstName) > However I want to write each line to a file first, so that I can > alphabetically sort the links by lastName, before printing them. You don't need a file (not even for readable programmer feedback) -- just a list. And sort the list. > I've found this snippet: > > # Let's create a file and write it to disk. > filename = "test.dat" > # Let's create some data: > done = 0 > namelist = [] > *while* *not* done: > name = raw_input("Enter a name:") > *if* type(name) == type(""): > namelist.append(name) > *else*: > *break* Things like if type(name) == type(""): is considered bad python idiom (for goood reasons). Prefere: if isinstance(name, basestring): which also has the advantage to let unicode pass in. > # Create a file object: > # in "write" mode > FILE = open(filename,"w") > FILE.writelines(namelist) > > # Alternatively > # for name in namelist: > # FILE.write(name) > > FILE.close() # this is icing, you can just exit and this will be > # handled automagically. > > source: http://www.penzilla.net/tutorials/python/fileio/ > > Going by the above snippet, I will need to write the hyperlink to a > variable, e.g. name, which is in turn appended to namelist, and > subsequently written to the file. How do I save this > > <a href=\"http://users.example.com/~" + userName + ">" + lastName + ", " > + firstName + "</a> > > to a variable, then? Answer above. > Thanks > > Dayo Denis ------ la vita e estrany _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor