question about python "casting"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sang park

    question about python "casting"

    Hi all - Just started learning Python this past week and had a question
    about using the lists in python with objects. (code attached below).

    basically, what i want to do is read an xml file, and load a list of Objects
    (in this case, TTSProject objects). the problem i see when i try to run it,
    is the following:

    name :
    Traceback (most recent call last):
    File "ProjectParser. py", line 63, in ?
    print "name : ", p.name
    AttributeError: 'NoneType' object has no attribute 'name'

    i was wondering what the proper way to accomplish this would be. any help
    would be appreciated.

    thanks,

    sang



    from xml.dom import minidom

    print "Starting"
    xmldoc = minidom.parse(' projects.xml')
    print "Getting the root node"
    root = xmldoc.childNod es
    projectlist = root[0].childNodes
    print "project list node"


    class TTSProject:
    def __init__(self):
    self.tags = {};

    projects = []

    def getText(nodes):
    for node in nodes:
    if node.nodeType == node.TEXT_NODE:
    return node.data

    def getProject (nodes):
    project = TTSProject()
    if nodes.nodeType == nodes.ELEMENT_N ODE:
    datanodes = info.childNodes
    dataStr = getText(datanod es)
    # print info.nodeName, ":", dataStr
    if dataStr == "name":
    project.name = dataStr
    if dataStr == "email":
    project.email = dataStr
    if dataStr == "admin":
    project.admin = dataStr
    if dataStr == "descriptio n":
    project.descrip tion = dataStr
    return project

    for p in projectlist:
    projectInfo = p.childNodes
    for info in projectInfo:
    project = getProject(info )
    projects.append (project)

    for proj in projects:
    print "name : ", proj.name
    print "email : ", proj.email
    print "admin : ", proj.admin
    print "descriptio n : ", proj.descriptio n


  • Martin v. Löwis

    #2
    Re: question about python "casting&q uot;

    sang park wrote:[color=blue]
    > name :
    > Traceback (most recent call last):
    > File "ProjectParser. py", line 63, in ?
    > print "name : ", p.name
    > AttributeError: 'NoneType' object has no attribute 'name'
    >
    > i was wondering what the proper way to accomplish this would be. any help
    > would be appreciated.[/color]

    Could it be that you have been executing a code different from
    the one you were posting? In your posted code, you have a variable
    called "proj", whereis from your exception, it appears the variable
    is called "p".
    [color=blue]
    > if nodes.nodeType == nodes.ELEMENT_N ODE:
    > datanodes = info.childNodes
    > dataStr = getText(datanod es)
    > # print info.nodeName, ":", dataStr
    > if dataStr == "name":
    > project.name = dataStr
    > if dataStr == "email":
    > project.email = dataStr
    > if dataStr == "admin":
    > project.admin = dataStr
    > if dataStr == "descriptio n":
    > project.descrip tion = dataStr[/color]

    It is unlikely that this does what you want: Most likely, you
    have elements "name", "email", etc in your XML document.
    You need to look at some node's nodeName, to find out whether
    it is a "name" node - looking at the text inside the node
    won't help.

    E.g. in your code above, if dataStr equals "email", you do

    project.email = dataStr

    Since dataStr is email, this is the same as

    project.email = "email"

    which is not what you want.

    Regards,
    Martin

    Comment

    Working...