通用XML大綱:美麗的湯提取物()的問題
<dasbhoards>
<dashboard name="S1>
<repository-location derived-from='http://dataviz.win.compete.com/workbooks/OTCSurvey_06_15_11_16_54/RT4?rev=' id='RT4' path='/workbooks/RetailFootwear' revision='' />
<style>
</style>
<zones>
<zone h='92975' id='4' param='horz' type='layout-flow' w='87842' x='12158' y='7025'>
<zone h='92975' id='2' type='layout-basic' w='77953' x='12158' y='7025'>
<zone h='92975' id='1' name='RT4_stk_bar_grid' w='77953' x='12158' y='7025'>
</zone>
</zone>
<zone fixed-size='170' h='92975' id='3' is-fixed='true' param='vert' type='layout-flow' w='9889' x='90111' y='7025'>
<zone h='13739' id='6' name='RT4_stk_bar_grid' param='[mysql.40611.854150011575].[none:response:nk]' type='color' w='9889' x='90111' y='7025'>
</zone>
</zone>
</zone>
<zone h='7025' id='7' name='Q-RT4' w='87842' x='12158' y='0'>
</zone>
<zone h='100000' id='9' param='vert' type='layout-flow' w='12158' x='0' y='0'>
<zone h='6818' id='5' name='RT4_stk_bar_grid' param='[mysql.40611.854150011575].[none:crosstab_group:nk]' type='filter' w='12158' x='0' y='0'>
</zone>
<zone h='31921' id='10' name='RT4_stk_bar_grid' param='[mysql.40611.854150011575].[none:question_base:nk]' type='filter' w='12158' x='0' y='6818'>
</zone>
</zone>
</zones>
</dashboard>
<dashboard name="S2">
<more tags>
</dashboard>
</dashboards>
這裏是我美麗的湯項目的工作流程。我找到所有的儀表板元素,並使用extract()刪除所有沒有「s1」作爲屬性「name」的值。 但問題在於,在書寫之前,似乎所有儀表板元素都將從最終的湯中刪除。 我做錯了什麼? 讓我的話說,有一個名稱=「S1」的儀表板元素。
#load the xml
workbook = open("C:\\Users\\rabdel.WINCMPT\\Documents\\Retail Footwear.twb")
soup = BeautifulStoneSoup(workbook, selfClosingTags=['repository-location', 'style'])
workbook.close()
#get all "dashboard" elements (children of "dashboards")
d = soup.findAll('dashboard')
#extract all but one
for child in d:
if child.get("name", "").lower() != "s1":
child.extract()
#write out the results
modified_workbook = open("C:\\Users\\rabdel.WINCMPT\\Documents\\Footwear.xml", "w")
modified_workbook.write(soup.prettify())
modified_workbook.close()
更多信息: 什麼是最有趣的是,如果我寫的儀表盤(父)元素之前,將萃取後的文件,我得到正是我期望的那樣。問題是,湯本身似乎是不同的。
您正在檢查代碼中的「s1」,但您在帖子中說「S1」。區分大小寫? – James
做一個不敏感的檢查:當我在等待答案時,這正是我所做的,而不是檢查:child.get(「name」,「」).lower()!=「s1」 – Ramy