从XML元素中删除空白字符使用ElementTree

huangapple go评论57阅读模式
英文:

Stripping whitespaces from an xml element using ElementTree

问题

在运行你的代码时,你遇到了以下错误:

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

这个错误是由于你的代码中某些元素的elem.textelem.tail的值为None,而None类型对象没有strip属性,所以无法调用strip方法。为了解决这个问题,你可以在调用strip方法之前检查elem.textelem.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.textelem.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>

huangapple
  • 本文由 发表于 2023年2月16日 08:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定