久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

即使在 pretty_print=True 時,使用 lxml 編寫也不會產

Writing with lxml emitting no whitespace even when pretty_print=True(即使在 pretty_print=True 時,使用 lxml 編寫也不會產生空格)
本文介紹了即使在 pretty_print=True 時,使用 lxml 編寫也不會產生空格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 lxml 庫來讀取 xml 模板,插入/更改一些元素,并保存生成的 xml.我使用 etree.Elementetree.SubElement 方法動態創建的元素之一:

I'm using the lxml library to read an xml template, insert/change some elements, and save the resulting xml. One of the elements which I'm creating on the fly using the etree.Element and etree.SubElement methods:

tree = etree.parse(r'xml_archive	emplatesmetadata_template_pts.xml')
root = tree.getroot()

stream = []
for element in root.iter():
    if isinstance(element.tag, basestring):
        stream.append(element.tag)

        # Find "keywords" element and insert a new "theme" element
        if element.tag == 'keywords' and 'theme' not in stream:
            theme = etree.Element('theme')
            themekt = etree.SubElement(theme, 'themekt').text = 'None'
            for tk in themekeys:
                themekey = etree.SubElement(theme, 'themekey').text = tk
            element.insert(0, theme)

很好地打印到屏幕上print etree.tostring(theme, pretty_print=True):

<theme>
  <themekt>None</themekt>
  <themekey>Hydrogeology</themekey>
  <themekey>Stratigraphy</themekey>
  <themekey>Floridan aquifer system</themekey>
  <themekey>Geology</themekey>
  <themekey>Regional Groundwater Availability Study</themekey>
  <themekey>USGS</themekey>
  <themekey>United States Geological Survey</themekey>
  <themekey>thickness</themekey>
  <themekey>altitude</themekey>
  <themekey>extent</themekey>
  <themekey>regions</themekey>
  <themekey>upper confining unit</themekey>
  <themekey>FAS</themekey>
  <themekey>base</themekey>
  <themekey>geologic units</themekey>
  <themekey>geology</themekey>
  <themekey>extent</themekey>
  <themekey>inlandWaters</themekey>
</theme>

但是,當使用 etree.ElementTree(root).write(out_xml_file, method='xml', pretty_print=True) 寫出 xml 時,此元素在輸出文件中被展平:

However, when using etree.ElementTree(root).write(out_xml_file, method='xml', pretty_print=True) to write out the xml, this element gets flattened in the output file:

<theme><themekt>None</themekt><themekey>Hydrogeology</themekey><themekey>Stratigraphy</themekey><themekey>Floridan aquifer system</themekey><themekey>Geology</themekey><themekey>Regional Groundwater Availability Study</themekey><themekey>USGS</themekey><themekey>United States Geological Survey</themekey><themekey>thickness</themekey><themekey>altitude</themekey><themekey>extent</themekey><themekey>regions</themekey><themekey>upper confining unit</themekey><themekey>FAS</themekey><themekey>base</themekey><themekey>geologic units</themekey><themekey>geology</themekey><themekey>extent</themekey><themekey>inlandWaters</themekey></theme>

文件的其余部分寫得很好,但是這個特殊的元素正在引起(純粹是審美的)麻煩.關于我做錯了什么的任何想法?

The rest of the file is written nicely, but this particular element is causing (purely aesthetic) trouble. Any ideas of what I'm doing wrong?

以下是來自模板 xml 文件的標記片段(將其保存為template.xml"以在底部與代碼片段一起運行).標簽的扁平化僅在我解析現有文件并插入新元素時發生,而不是在使用 lxml 從頭創建 xml 時發生.

Below is a snippet of markup from the template xml file (save this as "template.xml" to run with code snippet at bottom). The flattening of tags only occurs when I parse an existing file and insert a new element, not when the xml is created from scratch using lxml.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="fgdc_classic.xsl"?>
<metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://water.usgs.gov/GIS/metadata/usgswrd/fgdc-std-001-1998.xsd">
    <keywords>
       <theme>
            <themekt>ISO 19115 Topic Categories</themekt>
            <themekey>environment</themekey>
            <themekey>geoscientificInformation</themekey>
            <themekey>inlandWaters</themekey>
        </theme>
        <place>
            <placekt>None</placekt>
            <placekey>Florida</placekey>
            <placekey>Georgia</placekey>
            <placekey>Alabama</placekey>
            <placekey>South Carolina</placekey>
        </place>
    </keywords>

</metadata>

下面是與標記片段(上圖)一起使用的代碼片段:

Below is a snippet of code to be used with the snippet of markup (above):

# Create new theme element to insert into root
themekeys = ['Hydrogeology', 'Stratigraphy', 'inlandWaters']

tree = etree.parse(r'template.xml')
root = tree.getroot()

stream = []
for element in root.iter():
    if isinstance(element.tag, basestring):
        stream.append(element.tag)

        # Edit theme keywords
        if element.tag == 'keywords':
            theme = etree.Element('theme')
            themekt = etree.SubElement(theme, 'themekt').text = 'None'
            for tk in themekeys:
                themekey = etree.SubElement(theme, 'themekey').text = tk
            element.insert(0, theme)

# Write XML to new file
out_xml_file = 'test.xml'
etree.ElementTree(root).write(out_xml_file, method='xml', pretty_print=True)
with open(out_xml_file, 'r') as f:
    lines = f.readlines()

with open(out_xml_file, 'w') as f:
    f.write('<?xml version="1.0" encoding="UTF-8"?>
')
    for line in lines:
        f.write(line)

推薦答案

如果你替換這行:

tree = etree.parse(r'template.xml')

這些行:

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(r'template.xml', parser)

那么它將按預期工作.訣竅是使用具有 remove_blank_text 選項設置為 True.任何現有的可忽略空格都將被刪除,因此不會破壞后續的漂亮打印.

then it will work as expected. The trick is to use an XMLParser that has the remove_blank_text option set to True. Any existing ignorable whitespace will be removed and will therefore not disrupt the subsequent pretty-printing.

這篇關于即使在 pretty_print=True 時,使用 lxml 編寫也不會產生空格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 欧洲一区视频 | 国产色黄 | 一级毛片免费完整视频 | 中文字幕国产精品 | 久久久久久久久国产精品 | 欧美日韩国产中文 | 播放一级毛片 | 在线免费观看黄视频 | 风间由美一区二区三区在线观看 | 99久久久无码国产精品 | 欧美日韩网站 | 亚洲综合视频 | 久久av影院| 国产精品欧美一区二区三区不卡 | 国产午夜影院 | 欧美久久久网站 | 国产小网站 | 91久久精品一区二区二区 | 成人深夜福利网站 | www312aⅴ欧美在线看 | 日韩欧美精品一区 | 日韩高清一区二区 | 亚洲成人久久久 | 久久国产精品久久久久久 | 午夜精品久久久久久久久久久久久 | 国产男女猛烈无遮掩视频免费网站 | 成人福利视频 | 亚洲精品久久久久avwww潮水 | 99国产精品一区二区三区 | 亚洲精品一区二区三区中文字幕 | 久久久片| 亚洲精品一区二区三区在线 | 欧美日韩精品一区二区天天拍 | 国产98色在线 | 日韩 | 麻豆hd| 国产在线一区二区 | 欧美电影网 | 亚洲精品视频在线观看视频 | 一区二区精品视频 | 成年人国产在线观看 | 久久久久综合 |