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

即使在 pretty_print=True 時(shí),使用 lxml 編寫也不會(huì)產(chǎn)

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

問題描述

我正在使用 lxml 庫(kù)來讀取 xml 模板,插入/更改一些元素,并保存生成的 xml.我使用 etree.Elementetree.SubElement 方法動(dòng)態(tài)創(chuàng)建的元素之一:

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>

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

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>

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

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 文件的標(biāo)記片段(將其保存為template.xml"以在底部與代碼片段一起運(yùn)行).標(biāo)簽的扁平化僅在我解析現(xiàn)有文件并插入新元素時(shí)發(fā)生,而不是在使用 lxml 從頭創(chuàng)建 xml 時(shí)發(fā)生.

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>

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

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)

那么它將按預(yù)期工作.訣竅是使用具有 remove_blank_text 選項(xiàng)設(shè)置為 True.任何現(xiàn)有的可忽略空格都將被刪除,因此不會(huì)破壞后續(xù)的漂亮打印.

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.

這篇關(guān)于即使在 pretty_print=True 時(shí),使用 lxml 編寫也不會(huì)產(chǎn)生空格的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 成人精品国产一区二区4080 | 亚洲国产中文在线 | 色久五月| 成人精品一区二区三区四区 | 国产精品美女久久久久久免费 | 91精品www | 91在线一区二区三区 | 欧美videosex性极品hd | 国产三区精品 | 手机看片169 | 国内久久精品 | 国产成人精品亚洲日本在线观看 | 欧美99久久精品乱码影视 | 国内精品久久久久久影视8 最新黄色在线观看 | 欧美成人精品一区二区男人看 | 精品国偷自产在线 | 国产a级黄色录像 | 日韩精品一区二区三区中文字幕 | 国产成人免费 | 精品九九| 区一区二在线观看 | 999国产精品视频免费 | 美美女高清毛片视频免费观看 | 欧美视频在线播放 | 欧美成人激情视频 | 无吗视频 | 天天舔天天| av永久免费| 无人区国产成人久久三区 | 在线视频91 | 欧美国产中文 | 成人在线视频观看 | 日本一二三区电影 | 365夜爽爽欧美性午夜免费视频 | 久久51 | 91福利网| 午夜精品久久久久久久久久久久 | 欧美日韩在线精品 | 国产丝袜一区二区三区免费视频 | 亚洲网站在线观看 | 成人精品免费视频 |