我想從 XML 中獲取一些資訊。
- 如果 type-v2="text" 則為區域 ID
- 此特定區域下的所有孩子都隱藏在元素中。
這是 XML 代碼:
<dashboards>
<dashboard name="1 off">
<style/>
<size maxheight="800" maxwidth="1000" minheight="800" minwidth="1000"/>
<zones>
<zone h="100000" id="2" type-v2="layout-basic" w="100000" x="0" y="0">
<zone forceUpdate="true" h="98000" id="1" type-v2="text" w="49200" x="800" y="1000">
<formatted-text>
<run fontsize="1">
row 1
</run>
<run>
??
</run>
<run fontsize="2">
row 2
</run>
</formatted-text>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="4"/>
</zone-style>
</zone>
<zone h="98000" id="3" type-v2="text" w="49200" x="50000" y="1000">
<formatted-text>
<run>
id2
</run>
</formatted-text>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="4"/>
</zone-style>
</zone>
<zone-style>
<format attr="border-color" value="#000000"/>
<format attr="border-style" value="none"/>
<format attr="border-width" value="0"/>
<format attr="margin" value="8"/>
</zone-style>
</zone>
</zones>
</dashboard>
</dashboards>
我能夠獲得單獨運行的所有 ID 和所有詳細資訊,但我想獲得不同的輸出。因此,對于每個元素,我想為其分配父 ID。
這是我的代碼分別獲取資訊:
import xml.etree.ElementTree as et
tree = et.parse(r'C:\book3.twb')
root = tree.getroot()
dbnameRC=[]
fontRC = []
sizeRC = []
weightRC = []
f_styleRC = []
decoRC = []
colorRC= []
alignRC = []
textcontRC=[]
idRC=[]
for db in root.iter("dashboard"):
root1 = db
for z in root1.findall(".//zone[@type-v2='text']"):
idRC.append(z.get('id'))
for m in root1.findall(".//zone[@type-v2='text']/formatted-text//run"):
weightRC.append(m.get('bold'))
alignRC.append(m.get('fontalignment'))
colorRC.append(m.get('fontcolor'))
fontRC.append(m.get('fontname'))
sizeRC.append(m.get('fontsize'))
f_styleRC.append(m.get('italic'))
decoRC.append(m.get('underline'))
dbnameRC.append(db.attrib['name'])
textcontRC.append(m.text)
1. idRC 的輸出是:
['1', '3']
這是正確的,因為我們只有兩個用于 type-v2='text'] 的 id
- sizeRC 的輸出是
['1', None, '2', None]
這也是正確的。
問題是如何撰寫代碼以提供如下輸出:

基本上我要做的就是進入帶有 type-v2="text" 的區域,獲取它的 id 并在下面進行所有運行并將其分配給這個特定的 id,然后移動到具有不同 id 和
type-v2="text" 的另一個區域并在該區域下進行所有運行。
uj5u.com熱心網友回復:
而不是你的第二個root1.findall(),你可以zone.findall()代替 - 允許你對id每次運行進行分組。
runs = []
for db in root.iter("dashboard"):
root1 = db
for zone in root1.findall(".//zone[@type-v2='text']"):
idrc = zone.get('id')
for run in zone.findall("./formatted-text//run"):
runs.append([
idrc,
run.get("bold"),
run.get("fontalignment"),
run.get("fontcolor"),
run.get("fontname"),
run.get("fontsize"),
run.get("italic"),
run.get("underline"),
db.attrib["name"],
run.text
])
輸出:
>>> runs
[['1', None, None, None, None, '1', None, None, '1 off', 'row 1'],
['1', None, None, None, None, None, None, None, '1 off', '??'],
['1', None, None, None, None, '2', None, None, '1 off', 'row 2'],
['3', None, None, None, None, None, None, None, '1 off', 'id2']]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427390.html
