【问题标题】:AttributeError when trying to run simple ETL code尝试运行简单的 ETL 代码时出现 AttributeError
【发布时间】:2021-07-06 04:18:49
【问题描述】:

我已经阅读了大量其他主题,但找不到解决方案,因此请感谢任何人就如何解决此问题提出的意见!

完成一门 Python 数据工程课程,这个项目是关于网络抓取的。在尝试创建从 XML、CSV 和 JSON 文件中提取数据的函数时,我得到了下面的“AttributeError”。

AttributeError - Traceback(最近一次调用最后一次)在 1 log("提取阶段开始") ----> 2 提取数据 = 提取() 3 log("提取阶段结束") 4 提取数据

在 extract() 29 # 最后,处理所有的xml文件 30 用于 glob.glob("*.xml") 中的 xmlFile: ---> 31 提取数据 = 提取数据.append(xmlExtract(xmlFile), ignore_index=True) 32 33 返回提取数据

在 xmlExtract(xmlFile) 15 根 = 树.getroot() 16 对于根中的人: ---> 17 name = person.find("name").text 18 高度 = 浮动(person.find("height").text) 19 weight = float(person.find("weight").text)

AttributeError: 'NoneType' 对象没有属性 'text'

这是错误所指的代码sn-p:

def xmlExtract(xmlFile):
    dframe = pddf(columns=["name", "height", "weight"])
    tree = ET.parse(xmlFile)
    root = tree.getroot()
    for person in root:
        name = person.find("name").text
        height = float(person.find("height").text)
        weight = float(person.find("weight").text)
        dframe = dframe.append({"name":name, "height":height, "weight":weight}, ignore_index=True)
    return dframe

感谢任何关于从哪里开始的指示。

附: 'pdf' 是从 pandas -from pandas import DataFrame as pddf 独特导入的 pandas.dataframe - 因为我在使用 import pandas as pd 时遇到错误,然后使用 pd.dataframe。

【问题讨论】:

    标签: python-3.x attributeerror


    【解决方案1】:

    错误很明显:

    AttributeError: 'NoneType' object has no attribute 'text'
    

    .find 的一些调用返回None。在调用.text之前,您必须先检查该值是否为None

    def xmlExtract(xmlFile):
        dframe = pddf(columns=["name", "height", "weight"])
        tree = ET.parse(xmlFile)
        root = tree.getroot()
        for person in root:
            name = person.find("name").text if person.find("name") else None
            height = float(person.find("height").text) if person.find("height") else None
            weight = float(person.find("weight").text) if person.find("weight") else None
    
            dframe = dframe.append({"name": name, "height": height, "weight": weight}, ignore_index=True)
        return dframe
    

    这段代码不是最好的。这只是一个例子。

    【讨论】:

    • 谢谢麦孔!这允许数据框返回,但奇怪的是,在我从 XML、JSON 和 CSV 文件检索的内容之外还有很多行/列。至少这给了我一些现在可以进一步探索的东西。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    相关资源
    最近更新 更多