Очень простой парсинг XML в Python - PullRequest
0 голосов
/ 25 мая 2020

Я пытаюсь взять очень длинную xml версию электронной таблицы (.xlsx) и удалить одну «строку» (не уверен, что это за терминология). Я не планирую делать это снова, поэтому мне действительно не нужно понимать, что происходит, просто хочу, чтобы это было сделано.

В любом случае, в качестве небольшого примера, скажем, у меня есть этот файл xml :

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"><dimension ref="A1:C15"/><sheetViews><sheetView topLeftCell="B1" workbookViewId="0"><selection activeCell="B2" sqref="B2:C2"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight="14.25" x14ac:dyDescent="0.45"/><cols><col min="1" max="1" width="0" hidden="1" customWidth="1"/><col min="2" max="2" width="61" customWidth="1"/><col min="3" max="3" width="92" customWidth="1"/></cols><sheetData><row r="1" spans="1:3" s="4" customFormat="1" ht="115.5" customHeight="1" thickTop="1" thickBot="1" x14ac:dyDescent="0.5"><c r="A1" s="5"/><c r="B1" s="9"/><c r="C1" s="10"/></row><row r="2" spans="1:3" s="6" customFormat="1" ht="19.899999999999999" thickTop="1" thickBot="1" x14ac:dyDescent="0.6"><c r="B2" s="107" t="s"><v>307</v></c><c r="C2" s="108"/></row><row r="3" spans="1:3" ht="14.65" x14ac:dyDescent="0.45"><c r="B3" s="73" t="s"><v>308</v></c><c r="C3" s="73" t="s"><v>309</v></c></row><row r="4" spans="1:3" ht="43.9" x14ac:dyDescent="0.45"><c r="B4" s="74" t="s"><v>311</v></c><c r="C4" s="75" t="s"><v>339</v></c></row><row r="5" spans="1:3" ht="58.5" x14ac:dyDescent="0.45"><c r="B5" s="76" t="s"><v>333</v></c><c r="C5" s="75" t="s"><v>340</v></c></row><row r="6" spans="1:3" ht="29.25" x14ac:dyDescent="0.45"><c r="B6" s="74" t="s"><v>324</v></c><c r="C6" s="75" t="s"><v>341</v></c></row><row r="7" spans="1:3" ht="29.25" x14ac:dyDescent="0.45"><c r="B7" s="76" t="s"><v>267</v></c><c r="C7" s="75" t="s"><v>342</v></c></row><row r="8" spans="1:3" ht="14.65" x14ac:dyDescent="0.45"><c r="B8" s="76" t="s"><v>266</v></c><c r="C8" s="77" t="s"><v>343</v></c></row><row r="9" spans="1:3" ht="14.65" x14ac:dyDescent="0.45"><c r="B9" s="76" t="s"><v>332</v></c><c r="C9" s="75" t="s"><v>344</v></c></row><row r="10" spans="1:3" ht="117" x14ac:dyDescent="0.45"><c r="B10" s="76" t="s"><v>334</v></c><c r="C10" s="75" t="s"><v>345</v></c></row><row r="11" spans="1:3" ht="29.25" x14ac:dyDescent="0.45"><c r="B11" s="76" t="s"><v>335</v></c><c r="C11" s="75" t="s"><v>346</v></c></row><row r="12" spans="1:3" ht="29.25" x14ac:dyDescent="0.45"><c r="B12" s="76" t="s"><v>310</v></c><c r="C12" s="75" t="s"><v>347</v></c></row><row r="13" spans="1:3" ht="43.9" x14ac:dyDescent="0.45"><c r="B13" s="74" t="s"><v>312</v></c><c r="C13" s="75" t="s"><v>348</v></c></row><row r="14" spans="1:3" ht="15" customHeight="1" x14ac:dyDescent="0.45"><c r="B14" s="76" t="s"><v>336</v></c><c r="C14" s="75" t="s"><v>349</v></c></row><row r="15" spans="1:3" ht="58.5" x14ac:dyDescent="0.45"><c r="B15" s="76" t="s"><v>331</v></c><c r="C15" s="75" t="s"><v>350</v></c></row></sheetData><sheetProtection algorithmName="SHA-256" hashValue="MeLuWwuvSadTE2AfpsCLlDGDNMvG0JwGYZE8v0YTGJU=" saltValue="yrhU49K7PCR2uiiV5Olfqg==" spinCount="100000" sheet="1" objects="1" scenarios="1"/><sortState ref="B4:C15"><sortCondition ref="B4:B15"/></sortState><mergeCells count="1"><mergeCell ref="B2:C2"/></mergeCells><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup paperSize="9" orientation="portrait" r:id="rId1"/><drawing r:id="rId2"/></worksheet> 

Все, что я хочу сделать, это удалить следующую часть кода:

<sheetProtection algorithmName="SHA-256" hashValue="MeLuWwuvSadTE2AfpsCLlDGDNMvG0JwGYZE8v0YTGJU=" saltValue="yrhU49K7PCR2uiiV5Olfqg==" spinCount="100000" sheet="1" objects="1" scenarios="1"/>

А затем распечатать оставшуюся часть кода в прежнем виде.

Когда я пытаюсь сделать что-то вроде:

file = ET.parse('a.xml')
root = file.getroot()
for elem in root.findall("."):
    x = elem.find('sheetProtection')
    if x is not None:
            print("Found one!")
            elem.remove(check_elem)
file.write('b.xml')

Первая проблема: он не делает то, что я хочу - он все равно печатает.

Вторая проблема: вывод b. xml включает в себя все виды ns0, ns1 и c, которых раньше не было. Мне нужно, чтобы код был таким, каким был, чтобы его можно было прочитать в Excel.

Любая помощь была бы замечательной!

...