XML ElementTree打印精美

时间:2018-10-01 16:11:16

标签: python xml elementtree pretty-print

1。)如果查看以下标记和我的xml输出的空格,您会注意到空格不正确,这是2中代码的原因。

  • <SD-ID-Gesamt></SD-ID-Gesamt>
  • <Lage></Lage>
  • <Parameter></Parameter>

当前输出:

<SD-ID-Gesart>
    <SD-ID-Code>
        <SD-ID>DD-RE-1.0G-10</SD-ID>
        <Lage>
            <XP>14.84</XP>
            <YP>73.19</YP>
            <ZP>7.92</ZP>
            <WKS>95.32</WKS>
            </Lage>
        <Parameter>
            <Form>Rectangle</Form>
            <Art>Deckendurchbruch/Art>
            <Gewerk>-HT-SAN-DURCHBRUCH</Gewerk>
            <Ebene>1.OG OKFF +4,50 m</Ebene>
            <Breite>1,800000</Breite>
            <Höhe>0,500000</Höhe>
            <Tiefe>0,450000</Tiefe>
            <Status_Neu>0</Status_Neu>
            <Status_Geändert>1</Status_Geändert>
            </Parameter>
    <SD-ID-Code>

2。)我正在呼叫indent()以漂亮地打印我的xml文件。除了您在下面看到的代码中的一个小错误之外,它几乎可以按预期运行。您可以在上图中的xml-output中看到该错误。

def indent(elem, level=0):
i = "\n" + level*"  "
if len(elem):
    if not elem.text or not elem.text.strip():
        elem.text = i + ""
    if not elem.tail or not elem.tail.strip():
        elem.tail = i
for elem in elem:
    indent(elem, level+1)
    if not elem.tail or not elem.tail.strip():
        elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

3。)使用您接下来看到的代码,我在ElementTree通过indent()时创建了ElementTree。

indent(sd)
tree = ET.ElementTree(sd)
tree.write(open(pfad + '\\'+ datei_name + '.xml', "w"), encoding='utf-8', 
xml_declaration=True, method='xml')

我需要编辑indent()缩进函数的定义以获取正确的空格。在那种情况下,我真的不知道当标签相等时如何退回空格。如果您能帮助我解决该问题,我将非常高兴。

1 个答案:

答案 0 :(得分:1)

我无法从您的示例中复制错误的缩进,但是根据http://effbot.org/zone/element-lib.htm#prettyprint,您的函数被错误地复制了。对于这些行:

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup("""<html>...<script>{environment: "production",
                features: {}};
</script><script>
    Spotify = {};
    Spotify.Entity = {"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"followers":{"href":null,"total":273725},"genres":["art rock","brill building pop","british invasion","bubblegum pop","classic rock","folk rock","psychedelic rock","rock"],"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","popularity":60,"type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5","top_tracks":[{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/7K6JtyaSSVr7HidQsCHun0"},"href":"https:\/\/api.spotify.com\/v1\/albums\/7K6JtyaSSVr7HidQsCHun0","id":"7K6JtyaSSVr7HidQsCHun0","name":"Odessey And Oracle","release_date":"1968-04-19","release_date_precision":"day","total_tracks":12,"type":"album","uri":"spotify:album:7K6JtyaSSVr7HidQsCHun0"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":214026,"explicit":false,"external_ids":{"isrc":"GBCBS6732269"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/5AJrhrwz4oSZX2PwwV4qrN"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/5AJrhrwz4oSZX2PwwV4qrN","id":"5AJrhrwz4oSZX2PwwV4qrN","is_local":false,"is_playable":true,"name":"Time Of The Season (Mono Version)","popularity":68,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/31274768646a1b5fd860ab3fcdf6b830587b5702?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":12,"type":"track","uri":"spotify:track:5AJrhrwz4oSZX2PwwV4qrN"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/1vWVHhniWzqyNC0UzDDGs4"},"href":"https:\/\/api.spotify.com\/v1\/albums\/1vWVHhniWzqyNC0UzDDGs4","id":"1vWVHhniWzqyNC0UzDDGs4","name":"Begin Here","release_date":"1965-03-01","release_date_precision":"day","total_tracks":14,"type":"album","uri":"spotify:album:1vWVHhniWzqyNC0UzDDGs4"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":145293,"explicit":false,"external_ids":{"isrc":"GBCBS6432190"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/3dzW0SoVNyJTWWJimX8stj"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/3dzW0SoVNyJTWWJimX8stj","id":"3dzW0SoVNyJTWWJimX8stj","is_local":false,"is_playable":true,"name":"She's Not There","popularity":57,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/3a76321231fe16df6523a7af5d0b83ae23d7f566?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":7,"type":"track","uri":"spotify:track:3dzW0SoVNyJTWWJimX8stj"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/7K6JtyaSSVr7HidQsCHun0"},"href":"https:\/\/api.spotify.com\/v1\/albums\/7K6JtyaSSVr7HidQsCHun0","id":"7K6JtyaSSVr7HidQsCHun0","name":"Odessey And Oracle","release_date":"1968-04-19","release_date_precision":"day","total_tracks":12,"type":"album","uri":"spotify:album:7K6JtyaSSVr7HidQsCHun0"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":127733,"explicit":false,"external_ids":{"isrc":"GBCBS6732267"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/02KpkaZbiutsR0ucxce4Sh"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/02KpkaZbiutsR0ucxce4Sh","id":"02KpkaZbiutsR0ucxce4Sh","is_local":false,"is_playable":true,"name":"This Will Be Our Year","popularity":55,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/a93e9a62c991068702928d0a74a5bd0c1a3fc72f?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":9,"type":"track","uri":"spotify:track:02KpkaZbiutsR0ucxce4Sh"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/1vWVHhniWzqyNC0UzDDGs4"},"href":"https:\/\/api.spotify.com\/v1\/albums\/1vWVHhniWzqyNC0UzDDGs4","id":"1vWVHhniWzqyNC0UzDDGs4","images":[{"height":640,"url":"https:\/\/i.scdn.co\/image\/d5a749a17dfde0529b66ed73bdb979c1b71da74e","width":640},{"height":300,"url":"https:\/\/i.scdn.co\/image\/a2a119315824287969a97952acdec142b18cbad8","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/37149c0c69e4237181894c3ac621488f125469e7","width":64}],"name":"Begin Here","release_date":"1965-03-01","release_date_precision":"day","total_tracks":14,"type":"album","uri":"spotify:album:1vWVHhniWzqyNC0UzDDGs4"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":137733,"explicit":false,"external_ids":{"isrc":"GBCBS6432260"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/7egTKPYxbWaaV1jEzb1rHu"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/7egTKPYxbWaaV1jEzb1rHu","id":"7egTKPYxbWaaV1jEzb1rHu","is_local":false,"is_playable":true,"name":"Summertime","popularity":53,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/ad5fd12d797eca80f6da72bf37c74390710c699f?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":2,"type":"track","uri":"spotify:track:7egTKPYxbWaaV1jEzb1rHu"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/7K6JtyaSSVr7HidQsCHun0"},"href":"https:\/\/api.spotify.com\/v1\/albums\/7K6JtyaSSVr7HidQsCHun0","id":"7K6JtyaSSVr7HidQsCHun0","images":[{"height":600,"url":"https:\/\/i.scdn.co\/image\/0a645e0d3298508d0a090474d998789bd06bfbae","width":600},{"height":300,"url":"https:\/\/i.scdn.co\/image\/5a2d17c24e4babc3604466e601641bc6341a1f32","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/3c4308f9778895e9c35f9fd63cb0b33fdf3b3b05","width":64}],"name":"Odessey And Oracle","release_date":"1968-04-19","release_date_precision":"day","total_tracks":12,"type":"album","uri":"spotify:album:7K6JtyaSSVr7HidQsCHun0"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":235826,"explicit":false,"external_ids":{"isrc":"GBCBS6732201"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/5QXqz1KDz8Z8DmfQoxWmmy"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/5QXqz1KDz8Z8DmfQoxWmmy","id":"5QXqz1KDz8Z8DmfQoxWmmy","is_local":false,"is_playable":true,"name":"Care Of Cell 44","popularity":51,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/dd50eb2d932bf1429b614d6a2ee894fa9a0f9fbd?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":1,"type":"track","uri":"spotify:track:5QXqz1KDz8Z8DmfQoxWmmy"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/1vWVHhniWzqyNC0UzDDGs4"},"href":"https:\/\/api.spotify.com\/v1\/albums\/1vWVHhniWzqyNC0UzDDGs4","id":"1vWVHhniWzqyNC0UzDDGs4","images":[{"height":640,"url":"https:\/\/i.scdn.co\/image\/d5a749a17dfde0529b66ed73bdb979c1b71da74e","width":640},{"height":300,"url":"https:\/\/i.scdn.co\/image\/a2a119315824287969a97952acdec142b18cbad8","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/37149c0c69e4237181894c3ac621488f125469e7","width":64}],"name":"Begin Here","release_date":"1965-03-01","release_date_precision":"day","total_tracks":14,"type":"album","uri":"spotify:album:1vWVHhniWzqyNC0UzDDGs4"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":136026,"explicit":false,"external_ids":{"isrc":"GBCBS6432200"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/2o6sXX0llloUkaUm9DEbLY"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/2o6sXX0llloUkaUm9DEbLY","id":"2o6sXX0llloUkaUm9DEbLY","is_local":false,"is_playable":true,"name":"Can't Nobody Love You","popularity":47,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/f73cf78fefa69be6fe1872d735de2d324d646d09?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":9,"type":"track","uri":"spotify:track:2o6sXX0llloUkaUm9DEbLY"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/1vWVHhniWzqyNC0UzDDGs4"},"href":"https:\/\/api.spotify.com\/v1\/albums\/1vWVHhniWzqyNC0UzDDGs4","id":"1vWVHhniWzqyNC0UzDDGs4","images":[{"height":640,"url":"https:\/\/i.scdn.co\/image\/d5a749a17dfde0529b66ed73bdb979c1b71da74e","width":640},{"height":300,"url":"https:\/\/i.scdn.co\/image\/a2a119315824287969a97952acdec142b18cbad8","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/37149c0c69e4237181894c3ac621488f125469e7","width":64}],"name":"Begin Here","release_date":"1965-03-01","release_date_precision":"day","total_tracks":14,"type":"album","uri":"spotify:album:1vWVHhniWzqyNC0UzDDGs4"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":108573,"explicit":false,"external_ids":{"isrc":"GBCBS6432263"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/2iSxljzM8Xw9EEIGBRW1CD"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/2iSxljzM8Xw9EEIGBRW1CD","id":"2iSxljzM8Xw9EEIGBRW1CD","is_local":false,"is_playable":true,"name":"The Way I Feel Inside","popularity":45,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/5ada4402121ccad4aa791ce54450705e41b61dd5?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":4,"type":"track","uri":"spotify:track:2iSxljzM8Xw9EEIGBRW1CD"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/7K6JtyaSSVr7HidQsCHun0"},"href":"https:\/\/api.spotify.com\/v1\/albums\/7K6JtyaSSVr7HidQsCHun0","id":"7K6JtyaSSVr7HidQsCHun0","images":[{"height":600,"url":"https:\/\/i.scdn.co\/image\/0a645e0d3298508d0a090474d998789bd06bfbae","width":600},{"height":300,"url":"https:\/\/i.scdn.co\/image\/5a2d17c24e4babc3604466e601641bc6341a1f32","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/3c4308f9778895e9c35f9fd63cb0b33fdf3b3b05","width":64}],"name":"Odessey And Oracle","release_date":"1968-04-19","release_date_precision":"day","total_tracks":12,"type":"album","uri":"spotify:album:7K6JtyaSSVr7HidQsCHun0"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":139093,"explicit":false,"external_ids":{"isrc":"GBCBS6732193"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/6BSM7JqYn5wZxl1o3f7oxT"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/6BSM7JqYn5wZxl1o3f7oxT","id":"6BSM7JqYn5wZxl1o3f7oxT","is_local":false,"is_playable":true,"name":"A Rose For Emily","popularity":45,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/355dfc47758c00d89ffbcf9bbefb69af299549c7?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":2,"type":"track","uri":"spotify:track:6BSM7JqYn5wZxl1o3f7oxT"},{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/7K6JtyaSSVr7HidQsCHun0"},"href":"https:\/\/api.spotify.com\/v1\/albums\/7K6JtyaSSVr7HidQsCHun0","id":"7K6JtyaSSVr7HidQsCHun0","images":[{"height":600,"url":"https:\/\/i.scdn.co\/image\/0a645e0d3298508d0a090474d998789bd06bfbae","width":600},{"height":300,"url":"https:\/\/i.scdn.co\/image\/5a2d17c24e4babc3604466e601641bc6341a1f32","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/3c4308f9778895e9c35f9fd63cb0b33fdf3b3b05","width":64}],"name":"Odessey And Oracle","release_date":"1968-04-19","release_date_precision":"day","total_tracks":12,"type":"album","uri":"spotify:album:7K6JtyaSSVr7HidQsCHun0"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":163733,"explicit":false,"external_ids":{"isrc":"GBCBS6732195"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/3R2zCRTwqfD5DCX4XlAsoQ"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/3R2zCRTwqfD5DCX4XlAsoQ","id":"3R2zCRTwqfD5DCX4XlAsoQ","is_local":false,"is_playable":true,"name":"Beechwood Park (Mono Version)","popularity":43,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/812c91a7b15d365c6481f30c860d403484e05f20?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":4,"type":"track","uri":"spotify:track:3R2zCRTwqfD5DCX4XlAsoQ"},{"album":{"album_type":"compilation","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"external_urls":{"spotify":"https:\/\/open.spotify.com\/album\/0ttvz9n7vBPaMWpRplAZbs"},"href":"https:\/\/api.spotify.com\/v1\/albums\/0ttvz9n7vBPaMWpRplAZbs","id":"0ttvz9n7vBPaMWpRplAZbs","images":[{"height":600,"url":"https:\/\/i.scdn.co\/image\/96505f6b25ba6c5d48f017cbebe9f606729f0b97","width":600},{"height":300,"url":"https:\/\/i.scdn.co\/image\/edfaccd8ad5df41b427220bc5ec856368ad5b8d7","width":300},{"height":64,"url":"https:\/\/i.scdn.co\/image\/e13571723be8407c257fa1340c74fe1ebb96dbb8","width":64}],"name":"The Original Studio Recordings (Volume One)","release_date":"2007-04-30","release_date_precision":"day","total_tracks":20,"type":"album","uri":"spotify:album:0ttvz9n7vBPaMWpRplAZbs"},"artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/2jgPkn6LuUazBoBk6vvjh5"},"href":"https:\/\/api.spotify.com\/v1\/artists\/2jgPkn6LuUazBoBk6vvjh5","id":"2jgPkn6LuUazBoBk6vvjh5","name":"The Zombies","type":"artist","uri":"spotify:artist:2jgPkn6LuUazBoBk6vvjh5"}],"disc_number":1,"duration_ms":126600,"explicit":false,"external_ids":{"isrc":"GBCBS6432262"},"external_urls":{"spotify":"https:\/\/open.spotify.com\/track\/5FNRngWeBEpR8BiGLlNm2l"},"href":"https:\/\/api.spotify.com\/v1\/tracks\/5FNRngWeBEpR8BiGLlNm2l","id":"5FNRngWeBEpR8BiGLlNm2l","is_local":false,"is_playable":true,"name":"Tell Her No","popularity":41,"preview_url":"https:\/\/p.scdn.co\/mp3-preview\/e87062cab3f5ba1e17b3cc01395b9b4ce9c2d996?cid=162b7dc01f3a4a2ca32ed3cec83d1e02","track_number":10,"type":"track","uri":"spotify:track:5FNRngWeBEpR8BiGLlNm2l"}],"insights":{"artist_gid":"4bed34afbb0644ee84410e0774ee0b27","autobiography":{"body":"The 2nd UK band following <a href=\"spotify:artist:3WrFJ7ztbogyGnTHbHJFl2\" data-name=\"The Beatles\">The Beatles<\/a> to score a #1 hit in the US, <a href=\"spotify:artist:2jgPkn6LuUazBoBk6vvjh5\" data-name=\"The Zombies\">The Zombies<\/a> infiltrated airwaves with the sophisticated melodies, choral back-up harmonies and jazzy keyboard riffs of their 60s singles <a href=\"spotify:track:3dzW0SoVNyJTWWJimX8stj\" data-name=\"She's Not There\">She's Not There<\/a> and <a href=\"spotify:track:5FNRngWeBEpR8BiGLlNm2l\" data-name=\"Tell Her No\">Tell Her No<\/a>. Ironically the group broke-up just prior to achieving their greatest success\u2013 the worldwide chart-topping hit <a href=\"spotify:track:5AJrhrwz4oSZX2PwwV4qrN\" data-name=\"Time Of The Season (Mono Version)\">Time Of The Season (Mono Version)<\/a> from <a href=\"spotify:album:7K6JtyaSSVr7HidQsCHun0\" data-name=\"Odessey And Oracle\">Odessey And Oracle<\/a> ranked #100 in Rolling Stone\u2019s \u2018500 Greatest Albums of All Time.\u2019 To this day generations of new bands have cited the band&#39;s work as pop touchstones.\n \nFollowing the break-up of the original band, lead vocalist <a href=\"spotify:artist:27DCwiPx5ocQsf4K43SlIL\" data-name=\"Colin Blunstone\">Colin Blunstone<\/a> went on to develop an acclaimed solo career (<a href=\"spotify:track:20mUVwCxR1x2d5SuBRGW1F\" data-name=\"Say You Don't Mind\">Say You Don't Mind<\/a>, &quot;What Becomes of the Brokenhearted and <a href=\"spotify:track:1VD1jzThUoPSljwULNxbEV\" data-name=\"Old And Wise\">Old And Wise<\/a> with <a href=\"spotify:artist:2RtIpLf0XAoJH6ZqD4i2qQ\" data-name=\"The Alan Parson Project\">The Alan Parson Project<\/a>) and keyboardist\/songwriter Rod Argent rocked arenas with his band <a href=\"spotify:artist:46VosWAvtZsBl7rvxufsWG\" data-name=\"Argent\">Argent<\/a> ( <a href=\"spotify:track:3vfIv3pvYlLOa0wKIEuqFn\" data-name=\"Hold Your Head Up\">Hold Your Head Up<\/a>,&quot; &quot;<a href=\"spotify:track:1qUSBfdnI4rNBZPUwj0rxg\" data-name=\"God Gave Rock and Roll to You\">God Gave Rock and Roll to You<\/a>&quot;) but the legend of The Zombies continued to take on a life of its own. By the start of the new Millennium Blunstone and Argent were inspired to resurrect the band.\n \nThe release of 2015&#39;s <a href=\"spotify:album:59pVA8DVGTfziL0HJ07MR9\" data-name=\"Still Got That Hunger\">Still Got That Hunger<\/a> proved that Zombies fever is stronger than ever with premieres in Rolling Stone, Mojo, The Wall Street Journal, and Spin alongside broadcast performances on Later\u2026With Jools Holland and The Late Show With Stephen Colbert. It marked a historical moment on the Billboard charts as Odessey &amp;amp; Oracle re-entered the Billboard 100 48 years later at the same time as Still Got That Hunger!\n","urls":["https:\/\/en.wikipedia.org\/wiki\/The_Zombies"],"links":{"twitter":"https:\/\/twitter.com\/TheZombiesMusic","instagram":"https:\/\/instagram.com\/thezombiesofficial","wikipedia":"https:\/\/en.wikipedia.org\/wiki\/The_Zombies","facebook":"https:\/\/facebook.com\/thezombiesmusic"}},"header_image":{"id":"114df83ef8fb43ecbd250754a7eef627","uri":"https:\/\/i.scdn.co\/image\/05102b43a3f8cbe9be1d4e53936c3778c38d0058","width":2660,"height":1140},"images":[{"id":"dc83b09e2ff94ef7886d4a9952aad873","uri":"https:\/\/i.scdn.co\/image\/c054bc1404e67e75c71864fb7eadfcf2b29041a1","width":640,"height":640},{"id":"2468bcc482334def98c2376d6da6f944","uri":"https:\/\/i.scdn.co\/image\/c250625fd3906f8e48112d2f59628074e7f6be36","width":640,"height":640},{"id":"3c64a1688445434ebb7c9cf4faa26e2c","uri":"https:\/\/i.scdn.co\/image\/430e69db333d45a5db933df2dcb005950eccfca3","width":640,"height":640},{"id":"6e5c3926ca274ad1ab4144e01a209515","uri":"https:\/\/i.scdn.co\/image\/1fa01e8b770525b8d02ad57bceed4e73c9b6880e","width":640,"height":640},{"id":"6cd92979664a4ee5bcd3dd3e107bf780","uri":"https:\/\/i.scdn.co\/image\/8e2ceafe15e67248e6d903153ebe16e937ab4f81","width":640,"height":640},{"id":"85f80fb8369e48548fcc33b06bafa48a","uri":"https:\/\/i.scdn.co\/image\/d27b37a7399cd76b52f05a0833d8ca7e0e719cc1","width":640,"height":640},{"id":"4e10878d2c554185afc46f492864d8e3","uri":"https:\/\/i.scdn.co\/image\/fab1f745a76ab01899d366f60b33ffc3229e02d5","width":640,"height":640},{"id":"ecb2c436028d41a8bbeec81a64a983c7","uri":"https:\/\/i.scdn.co\/image\/69156bbf0749feb4f5f26df2d6f60eb326324104","width":640,"height":640},{"id":"15d319d1ba534f7593450c60db6a1bf3","uri":"https:\/\/i.scdn.co\/image\/0c74e4d0fc90493ef43805be15632e1bb6ec937f","width":640,"height":640},{"id":"84a3dde02bba447faeeda715aac3301e","uri":"https:\/\/i.scdn.co\/image\/f0b4757d3ebc0bb9d2dce9d59496c0dfd7969355","width":640,"height":640},{"id":"d0691a830aff410d8e5886995f6120f6","uri":"https:\/\/i.scdn.co\/image\/2a750b8443331627ac9cdfff9d246b8b28d25f6d","width":640,"height":640},{"id":"ce34afe64fe846c0971971b69731a1ad","uri":"https:\/\/i.scdn.co\/image\/7d06e4113f7ec3632f5a689b7a14f5cf2dbd2d97","width":640,"height":640}],"global_chart_position":0,"monthly_listeners":1492664,"monthly_listeners_delta":-37419,"follower_count":274041,"following_count":0,"playlists":{"entries":[{"uri":"spotify:user:spotify:playlist:37i9dQZF1DXaKIA8E7WcJj","name":"All Out 60s","image_url":"https:\/\/pl.scdn.co\/images\/pl\/default\/7d83fe0cbafd08b5572718f353c71025b84244be","owner":{"name":"Spotify","uri":"spotify:user:spotify"},"listeners":162030,"description":"The essential tracks from a decade of revolutions."},{"uri":"spotify:user:spotify:playlist:37i9dQZF1DWSDoVybeQisg","name":"Psychedelic Rock","image_url":"https:\/\/pl.scdn.co\/images\/pl\/default\/539d8f61c37764d6b70f0fd48165a219faf155d8","owner":{"name":"Spotify","uri":"spotify:user:spotify"},"listeners":38865,"description":"Zone out to some of the best psychedelic rock classics."},{"uri":"spotify:user:spotify:playlist:37i9dQZF1DXcyVNt8nnPfR","name":"Chilled Classics","image_url":"https:\/\/pl.scdn.co\/images\/pl\/default\/d511f580d51e4094694d280a94c2e4f260152675","owner":{"name":"Spotify","uri":"spotify:user:spotify"},"listeners":27954,"description":"Relax and allow your mind wander with these seriously laid-back classics."},{"uri":"spotify:user:1225379340:playlist:54a4Lsi1OdU4T4XV3ysQBU","name":"Vietnam War Era Music","image_url":"https:\/\/pl.scdn.co\/images\/pl\/default\/2835d6264ec8fde2aaabfb3ec937582018ef13b2","owner":{"name":"Trevor Johnston","uri":"spotify:user:1225379340"},"listeners":27530},{"uri":"spotify:user:spotify:playlist:37i9dQZF1DX5qNE4zrflL7","name":"Alternative 60s","image_url":"https:\/\/pl.scdn.co\/images\/pl\/default\/4fe7dcd7917b77e67897d4177e384cb864793de0","owner":{"name":"Spotify","uri":"spotify:user:spotify"},"listeners":25356,"description":"The songs that influenced everything that has happened since. Cover: The Rolling Stones"}]},"cities":[{"country":"US","region":"CA","city":"Los Angeles","listeners":43000},{"country":"GB","region":"ENG","city":"London","listeners":35183},{"country":"MX","region":"CMX","city":"Mexico City","listeners":27445},{"country":"US","region":"IL","city":"Chicago","listeners":24011},{"country":"US","region":"NY","city":"New York City","listeners":22582},{"country":"US","region":"CA","city":"San Francisco","listeners":16758},{"country":"US","region":"NY","city":"Brooklyn","listeners":15868},{"country":"US","region":"TX","city":"Dallas","listeners":14429},{"country":"FR","region":"75","city":"Paris","listeners":13131},{"country":"CA","region":"ON","city":"Toronto","listeners":12914},{"country":"US","region":"GA","city":"Atlanta","listeners":12524},{"country":"CL","region":"RM","city":"Santiago","listeners":12452},{"country":"BR","region":"SP","city":"S\u00e3o Paulo","listeners":12403},{"country":"US","region":"TX","city":"Houston","listeners":12353},{"country":"US","region":"WA","city":"Seattle","listeners":11244},{"country":"SE","region":"AB","city":"Stockholm","listeners":10880},{"country":"ES","region":"M","city":"Madrid","listeners":10233},{"country":"US","region":"CO","city":"Denver","listeners":10013},{"country":"AU","region":"QLD","city":"Brisbane","listeners":9141},{"country":"AU","region":"VIC","city":"Melbourne","listeners":9014},{"country":"NZ","region":"AUK","city":"Auckland","listeners":8996},{"country":"IE","region":"L","city":"Dublin","listeners":8832},{"country":"AU","region":"NSW","city":"Sydney","listeners":8590},{"country":"DE","region":"BE","city":"Berlin","listeners":8559},{"country":"US","region":"OR","city":"Portland","listeners":8433},{"country":"CA","region":"QC","city":"Montreal","listeners":8397},{"country":"US","region":"PA","city":"Philadelphia","listeners":7958},{"country":"AU","region":"WA","city":"Perth","listeners":7606},{"country":"NO","region":"03","city":"Oslo","listeners":7487},{"country":"ES","region":"B","city":"Barcelona","listeners":7460},{"country":"US","region":"AZ","city":"Phoenix","listeners":6998},{"country":"AR","region":"C","city":"Buenos Aires","listeners":6621},{"country":"US","region":"DC","city":"Washington","listeners":6560},{"country":"NL","region":"NH","city":"Amsterdam","listeners":6456},{"country":"US","region":"CA","city":"Sacramento","listeners":6139},{"country":"US","region":"NY","city":"The Bronx","listeners":5933},{"country":"US","region":"VA","city":"Arlington","listeners":5827},{"country":"US","region":"MN","city":"Minneapolis","listeners":5782},{"country":"US","region":"TX","city":"Austin","listeners":5568},{"country":"US","region":"FL","city":"Orlando","listeners":5496},{"country":"DE","region":"HH","city":"Hamburg","listeners":5488},{"country":"DK","region":"84","city":"Copenhagen","listeners":5486},{"country":"CA","region":"BC","city":"Vancouver","listeners":5448},{"country":"US","region":"OH","city":"Cleveland","listeners":5446},{"country":"US","region":"MA","city":"Boston","listeners":5357},{"country":"IT","region":"MI","city":"Milan","listeners":5039},{"country":"PE","region":"LMA","city":"Lima","listeners":5007},{"country":"US","region":"CA","city":"Riverside","listeners":4648},{"country":"SE","region":"O","city":"Gothenburg","listeners":4595},{"country":"US","region":"CA","city":"Oakland","listeners":4515}]}};
  </script><script src="https://open.scdn.co/static/landing_page_acq.8c8c93dc.js"></script><script src="https://vt.myvisualiq.net/2/afTxMmlGwCNRJiC5Bd75ug%3D%3D/vt-150.js" async=""></script><!-- Begin comScore Tag --><script>
var _comscore = _comscore || [];
_comscore.push({ c1: "2", c2: "15654041" });
(function() {
  var s = document.createElement("script"), el =
    document.getElementsByTagName("script")[0]; s.async = true;
  s.src = "https://sb.scorecardresearch.com/beacon.js";
  el.parentNode.insertBefore(s, el);
})();
</script><!-- End comScore Tag --></body></html>""", "html.parser")
pattern = re.compile(r"\s+=\s+(\{.*?\});\n")
result = soup.find("script", text=pattern)

引号之间应有两个空格:

if not elem.text or not elem.text.strip():
    elem.text = i + ""

我运行了这段代码,它可以正确显示。

if not elem.text or not elem.text.strip():
  elem.text = i + "  "

输出:

from xml.etree import ElementTree as et

def indent(elem, level=0):
  i = "\n" + level*"  "
  if len(elem):
    if not elem.text or not elem.text.strip():
      elem.text = i + "  "
    if not elem.tail or not elem.tail.strip():
      elem.tail = i
    for elem in elem:
      indent(elem, level+1)
    if not elem.tail or not elem.tail.strip():
      elem.tail = i
  else:
    if level and (not elem.tail or not elem.tail.strip()):
      elem.tail = i

data = '''<one><two><three>3</three><four>4</four></two></one>'''
tree = et.fromstring(data)
indent(tree)
et.dump(tree)

未来注意事项:

  • 文本图像无法复制,因此我将其作为练习供您在自己的XML上进行测试。
  • 精确粘贴精确代码并以文本形式输入数据 以重现问题,从而使答案易于重现。