英文:
Stripping whitespaces from an xml element using ElementTree
问题
在运行你的代码时,你遇到了以下错误:
AttributeError: 'NoneType' object has no attribute 'strip'
这个错误是由于你的代码中某些元素的elem.text
或elem.tail
的值为None
,而None
类型对象没有strip
属性,所以无法调用strip
方法。为了解决这个问题,你可以在调用strip
方法之前检查elem.text
和elem.tail
是否为None
,如果是None
,则跳过它们。以下是修改后的代码示例:
# Remove whitespace for each element in the tree
for elem in root.iter():
if elem.text is not None:
elem.text = elem.text.strip()
if elem.tail is not None:
elem.tail = elem.tail.strip()
这样修改后,代码会检查elem.text
和elem.tail
是否为None
,只有在它们不为None
时才会调用strip
方法,避免了出现AttributeError。
英文:
I'm having difficulty removing leading and trailing whitespace, even white space between elements that are deemed excessive. For the sake of the example, this is the xml document I'm currently running test cases on:
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
<description>Liechtenstein has a lot of flowers. </description>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
<description>Singapore has a lot of street markets.</description>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
<description> Panama has a lot of great food.</description>
</country>
</data>
Notice how in description for country name = "Liechtenstein" there is excess whitespace at the end of the description or excess white space between neighbor and description in the second country element or excess leading whitespace in description of the third country node.
Every time I run my code:
# Remove whitespace for each element in the tree
for elem in root.iter():
elem.text = elem.text.strip()
elem.tail = elem.tail.strip()
I end up with the following error:
AttributeError: 'NoneType' object has no attribute 'strip'
答案1
得分: 0
import xml.etree.ElementTree as ET
file = 'source.xml'
root = ET.parse(file)
for elem in root.iter():
if elem.text is not None:
elem.text = elem.text.strip()
if elem.tail is not None:
elem.tail = elem.tail.strip()
# print XML with stripped out whitespace
ET.dump(root)
# pretty print XML with stripped out whitespace
ET.indent(root, space="\t", level=0)
ET.dump(root)
英文:
import xml.etree.ElementTree as ET
file = 'source.xml'
root = ET.parse(file)
for elem in root.iter():
if elem.text is not None:
elem.text = elem.text.strip()
if elem.tail is not None:
elem.tail = elem.tail.strip()
# print XML with stripped out whitespace
ET.dump(root)
# pretty print XML with stripped out whitespace
ET.indent(root, space="\t", level=0)
ET.dump(root)
Output (stripped out whitespace):
<data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E" /><neighbor name="Switzerland" direction="W" /><description>Liechtenstein has a lot of flowers.</description></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N" /><description>Singapore has a lot of street markets.</description></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W" /><neighbor name="Colombia" direction="E" /><description>Panama has a lot of great food.</description></country></data>
Output (pretty-printed with stripped out whitespace):
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E" />
<neighbor name="Switzerland" direction="W" />
<description>Liechtenstein has a lot of flowers.</description>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N" />
<description>Singapore has a lot of street markets.</description>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W" />
<neighbor name="Colombia" direction="E" />
<description>Panama has a lot of great food.</description>
</country>
</data>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论