[英]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:
这是我的提示:
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)
都不需要存在此行 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()
内置方法,就像您在上面几行所做的那样。 Hope this hints will help you through. 希望这些提示可以帮助您。 Enjoy coding.
享受编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.