#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    49
    Rep Power
    10

    how to print out


    hi ,
    i have this piece of code which gets elements from xml file. then i put every element in their respective lists.

    Code:
        urlToFile = urllib.urlopen(url)
        str = urlToFile.read()
        doc = xml.dom.minidom.parseString(str)
    
    
        # Henter ut elemntene med navnet på taggen rundt
        # Oppretter en liste for senere bruk
        
        tit = doc.getElementsByTagName('title')
        tittel = []
        len = doc.getElementsByTagName('link')
        lenke = []
        des = doc.getElementsByTagName('description')
        beskrivelse = []
    
    
    
    #a varaible which gets a special value from the document
        
        t = tit[1].childNodes[0].nodeValue.encode("iso-8859-1")
    
    
        # put title, lenke and beskrivelse in each lists
        	
        for i in tit:
            tittel.append(i.childNodes[0].nodeValue.encode("iso-8859-1")) 
        for i in len:
            lenke.append(i.childNodes[0].nodeValue.encode("iso-8859-1")) 
        for i in des:
            beskrivelse.append(i.childNodes[0].nodeValue.encode("iso-8859-1"))
    the problem is that i dont exactly now how to print out the values stored in the lists ??
    how can i do that ???

    thanks
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The most common way to do this is to iterate over the values in the list and print them, either by value of by index. Alternatively you can use the string types join() method or print them using there individual index value: this requires you to have some idea about the length of the list before hand.

    Code:
    >>> aList = ['some', 'value', 'in', 'a', 'list']
    >>> for value in aList:
    ...     print value
    ...
    some
    value
    in
    a
    list
    >>> for value in aList:
    ...     print value,
    ...
    some value in a list
    >>>
    Code:
    >>> for index in range(len(aList)):
    ...     print aList[index]
    ...
    some
    value
    in
    a
    list
    >>>
    >>> for index, value in enumerate(aList):
    ...     print index, '=', value
    ...
    0 = some
    1 = value
    2 = in
    3 = a
    4 = list
    >>>
    Code:
    >>> print ' '.join(aList)
    some value in a list
    >>> print '\n'.join(aList)
    some
    value
    in
    a
    list
    >>>
    Code:
    >>> print aList[0], aList[1], aList[2], aList[3], aList[4]
    some value in a list
    >>> print aList[0], aList[1], aList[2], aList[3], aList[4], aList[5]
    some value in a list
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IndexError: list index out of range
    >>>
    Note: the comma at the end of the print statement in the second loop above. This simply prints a space instead of the standard newline character. Other than spotting that its all pretty self explanatory .

    You should also try searching this forum for more examples if you want them.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    49
    Rep Power
    10
    hi,
    thanks, i have three lists, title, link and description about each newspaper. i wants to print out title followed by link and description each of them. i want it to print out on this form:
    title
    <a href="link">link</a>
    description

    any suggestions, how to join these three lists ???

    thanks,
    ain
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If the list are the same length then you can use the same index to reference all three, although I think a much better way is to use the built-in zip() function .

    Code:
    >>> tList = ['title1', 'title2', 'title3']
    >>> lList = ['addy1', 'addy2', 'addy3', 'addy4']
    >>> dList = ['sample1', 'sample2', 'sample3']
    >>> 
    >>> for t, l, d in zip(tList, lList, dList):
    ...     print '<a href="%s">%s</a>: %s' % (l, t, d)
    ... 
    <a href="addy1">title1</a>: sample1
    <a href="addy2">title2</a>: sample2
    <a href="addy3">title3</a>: sample3
    >>>
    Note: zip() ignored the extra address in the lList, only iterating over the shared value in the list .

    Code:
    >>> zip(tList, lList, dList)
    [('title1', 'addy1', 'sample1'), ('title2', 'addy2', 'sample2'), ('title3', 'addy3', 'sample3')]
    >>>
    If you haven't used string formatting/interpolation before then you might want to brush up on it; there was a thread a little while ago on this. Just search the forum and you'll find all the info you need .

    Enjoy,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    49
    Rep Power
    10
    thanks neytan !

IMN logo majestic logo threadwatch logo seochat tools logo