Beautiful Soup extract() problem
GENERAL XML OUTLINE:
<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>
Here is the workflow for my beautiful soup project. I find all the dashboard elements and use extract() to remove all the ones that don't have "s1" as the value for the attribute "name". The problem though is that it seems ALL of the dashboard elements are being removed from the final soup before writing. Am I doing something wrong? Take my word that there IS a dashboard element with name="S1".
#load the xml
workbook = open("C:\\Users\\rabdel.WINCMPT\\Documents\\Retail Footwear.twb")
soup = BeautifulSt开发者_StackOverflow中文版oneSoup(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()
MORE INFO: what's most interesting is that if I write the dashboards (parent) element to file before and after the extract, i get EXACTLY what I expect. The problem is that the soup itself seems to be different.
Your code looks alright. It is impossible to tell why you don't get the expected result without seeing your XML file.
You might want to add a debug line to your loop like, such as:
for child in d:
name = child.get('name', '').lower()
print 'Name: "{0}"; Equal to "s1": {1}'.format(name, name == 's1')
...and make sure that there really is a tag with the name you are looking for!
this seems to actually not be a BeautifulSoup problem. The problem lies with the fact that the XML that's being generated is not being recognized by the application (Tabeleau) as valid xml.
精彩评论