[Tutor] writing HTML code to a variable/file

Dayo Adewunmi contactdayo at gmail.com
Tue May 26 16:57:10 CEST 2009


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>"

However I want to write each line to a file first, so that I can 
alphabetically sort the links by lastName, before printing them.

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*
    
    # 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?

Thanks

Dayo


More information about the Tutor mailing list