简体   繁体   English

我正在尝试将具有一些空属性的xml文件导入表。 收到此错误AttributeError:'NoneType'对象没有属性'strip'

[英]I am trying to import xml file with some empty attributes to table. getting this error AttributeError: 'NoneType' object has no attribute 'strip'

def XML_get_fields_and_types_and_data_levels_3(xml_file_name):
    data_2d = []
    for child in root:
        grandchildren = child.findall(".//")
        fields = []
        types = []
        data_1d = []
        data_2d.append(data_1d)
        for grandchild in grandchildren:
            data_1d.append(convert_string_to_type(grandchild.text))
        if grandchild.tag not in fields:
            fields.append(grandchild.tag)
            types.append(get_type_of_string(grandchild.text))
    return (fields, types, data_2d)

def get_type_of_string(string):
    clean_string = string.strip()
    try:
        if  clean_string is not None:
            clean_string = string.strip()
            return string.strip()
        if "." in clean_string:
            clean_string = string.split()
            if isinstance(clean_string, list):
                point_or_segment = [float(i) for i in clean_string]
                if len(point_or_segment) == 2:
                    return 'POINT'
                else:
                    return 'LSEG'
            else:
                val = float(clean_string)
                return 'REAL'
        else:
            val = int(clean_string)
            return 'INTEGER'
    except ValueError:
        return 'TEXT'

the issue is the line-of-code (loc) after your method definition, 问题是方法定义后的代码行(loc),

def get_type_of_string(string): clean_string = string.strip()

there string might be None , so the exception is raised. 那里的string可能是None ,所以引发了异常。 Instead of re-writing the method for you, which would easy for me but not be very helpful for you, I suggest you to re-design this method. 建议您重新设计此方法,而不是为您重新编写该方法(这对我来说很方便,但对您没有太大帮助)。 Here are my hints: 这是我的提示:

  • there is duplicated code 有重复的代码
  • the split() method always returns a list, no matter if the separator is found, isn't, so there is no need for this line to exists if isinstance(clean_string, list) split()方法总是返回一个列表,无论是否找到分隔符, if isinstance(clean_string, list)都不需要存在此行
  • then why is this conversion in place if then val is not used thereafter? 那么如果此后不使用val为什么要进行这种转换呢? the easiest way to evaluate a variable type is by using the isinstance() builtin method as you did it few lines above. 最简单的评估变量类型的方法是使用isinstance()内置方法,就像您在上面几行所做的那样。
  • try to split this method, in simpler and smaller methods, or try to simplify its logic. 尝试将此方法拆分为更简单和较小的方法,或者尝试简化其逻辑。

Hope this hints will help you through. 希望这些提示可以帮助您。 Enjoy coding. 享受编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不断收到错误:AttributeError: 'NoneType' object has no attribute 'strip' - I keep getting the error: AttributeError: 'NoneType' object has no attribute 'strip' 获取 AttributeError: 'NoneType' object 在读取 XML 时没有属性 'strip' - Getting AttributeError: 'NoneType' object has no attribute 'strip' while reading XML 为什么我会收到“AttributeError: 'NoneType' object has no attribute 'get'” - why am i getting "AttributeError: 'NoneType' object has no attribute 'get' " 为什么我收到“AttributeError: 'NoneType' object has no attribute 'send'”错误 - Why am I getting a `AttributeError: 'NoneType' object has no attribute 'send'` Error 我收到类似 AttributeError: 'NoneType' object has no attribute 'text' 的错误 - I am getting error like AttributeError: 'NoneType' object has no attribute 'text' 为什么我在执行 PCA 时遇到此错误:- AttributeError: 'NoneType' object has no attribute 'split' - Why i am getting this error while doing PCA :- AttributeError: 'NoneType' object has no attribute 'split' 我在“AttributeError:'NoneType'对象中没有属性'get_all_permissions'中收到这些错误 - I am getting these error in " AttributeError: 'NoneType' object has no attribute 'get_all_permissions' 为什么我在 /admin/main/consultation/'NoneType' object 没有属性 'lastname' 处出现 AttributeError 错误 - why am i getting error of AttributeError at /admin/main/consultation/ 'NoneType' object has no attribute 'lastname' AttributeError: 'NoneType' object 没有属性 'attrs',我在尝试从 url 抓取图像时遇到此错误。 我该如何解决? - AttributeError: 'NoneType' object has no attribute 'attrs', I am getting this error while I am trying to scrape images from the url. How can I fix it? 我该如何解决这个问题:AttributeError: 'NoneType' object has no attribute 'strip - How can i fix this :AttributeError: 'NoneType' object has no attribute 'strip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM