简体   繁体   中英

Remove a String Section from a file in python, with dynamic string matching?

Hi i have a big xml(See in end) file which is dynamically generated every time. So what i want to do is delete a particular section of line but the thing is see the following example: Out of the big xml file i need to delete this particular section through python:

<p:modifyVerifier cryptProviderType="rsaAES" cryptAlgorithmClass="hash" cryptAlgorithmType="typeAny" cryptAlgorithmSid="14" spinCount="100000" saltData="B8OqF9PY5YtJrJhHXdG2rg=="
 hashData="28wWrnLt7bZyEQJkDW3eIf0gkY8cGlJtMcy0fr4VoBQREbp2gg4l8d7H3vzTx8F3KsGCxys8pGz/n0lv+G9XOg=="/>

But here the hashData and saltData changes every time so hard-coded string matching approach wont work!

<p:modifyVerifier cryptProviderType="rsaAES" cryptAlgorithmClass="hash" cryptAlgorithmType="typeAny" cryptAlgorithmSid="14" spinCount="100000" saltData="9cRceofvEcHNy86qpf/jaA=="
 hashData="CWQjhCx80vjXRgWTJZCylIHIghBgLpWkcSlcBOZ8dNu5bMdqSCRoW0gt/8mM0jRN429HRrA+Ix4TRtAtQtrTag=="/>

And also this is not a different line ie it is joint string without spaces. For example there are more data before and after of the string without any space example:

</p:defaultTextStyle><p:modifyVerifier cryptProviderType="rsaAES" cryptAlgorithmClass="hash" cryptAlgorithmType="typeAny" cryptAlgorithmSid="14" spinCount="100000" saltData="B8OqF9PY5YtJrJhHXdG2rg==" hashData="28wWrnLt7bZyEQJkDW3eIf0gkY8cGlJtMcy0fr4VoBQREbp2gg4l8d7H3vzTx8F3KsGCxys8pGz/n0lv+G9XOg=="/><p:extLst>

Notice the </p:defaultTextStyle> without space before the <p:modifyVerifier I need the </p:defaultTextStyle> and remove the section mentioned above and also need the code afterwards.

So how do i first match a string with the following section with dynamically changing string in python! So normal hard-coding of string and matching wont work!

And also how do i delete the section after finding it from the xml file python.

this is the full file

<?xml version="1.0" encoding="UTF-8"?>
<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" showSpecialPlsOnTitleSld="0" saveSubsetFonts="1">
   <p:sldMasterIdLst>
      <p:sldMasterId id="2147483648" r:id="rId1" />
   </p:sldMasterIdLst>
   <p:notesMasterIdLst>
      <p:notesMasterId r:id="rId15" />
   </p:notesMasterIdLst>
   <p:handoutMasterIdLst>
      <p:handoutMasterId r:id="rId16" />
   </p:handoutMasterIdLst>
   <p:sldIdLst>
      <p:sldId id="569" r:id="rId2" />
      <p:sldId id="362" r:id="rId3" />
      <p:sldId id="560" r:id="rId4" />
      <p:sldId id="561" r:id="rId5" />
      <p:sldId id="566" r:id="rId6" />
      <p:sldId id="562" r:id="rId7" />
      <p:sldId id="563" r:id="rId8" />
      <p:sldId id="565" r:id="rId9" />
      <p:sldId id="564" r:id="rId10" />
      <p:sldId id="567" r:id="rId11" />
      <p:sldId id="568" r:id="rId12" />
      <p:sldId id="540" r:id="rId13" />
      <p:sldId id="557" r:id="rId14" />
   </p:sldIdLst>
   <p:sldSz cx="9144000" cy="6858000" type="screen4x3" />
   <p:notesSz cx="6858000" cy="9144000" />
   <p:defaultTextStyle>
      <a:defPPr>
         <a:defRPr lang="en-US" />
      </a:defPPr>
      <a:lvl1pPr marL="0" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl1pPr>
      <a:lvl2pPr marL="457200" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl2pPr>
      <a:lvl3pPr marL="914400" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl3pPr>
      <a:lvl4pPr marL="1371600" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl4pPr>
      <a:lvl5pPr marL="1828800" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl5pPr>
      <a:lvl6pPr marL="2286000" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl6pPr>
      <a:lvl7pPr marL="2743200" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl7pPr>
      <a:lvl8pPr marL="3200400" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl8pPr>
      <a:lvl9pPr marL="3657600" algn="l" defTabSz="914400" rtl="0" eaLnBrk="1" latinLnBrk="0" hangingPunct="1">
         <a:defRPr sz="1800" kern="1200">
            <a:solidFill>
               <a:schemeClr val="tx1" />
            </a:solidFill>
            <a:latin typeface="+mn-lt" />
            <a:ea typeface="+mn-ea" />
            <a:cs typeface="+mn-cs" />
         </a:defRPr>
      </a:lvl9pPr>
   </p:defaultTextStyle>
   <p:modifyVerifier cryptProviderType="rsaAES" cryptAlgorithmClass="hash" cryptAlgorithmType="typeAny" cryptAlgorithmSid="14" spinCount="100000" saltData="B8OqF9PY5YtJrJhHXdG2rg==" hashData="28wWrnLt7bZyEQJkDW3eIf0gkY8cGlJtMcy0fr4VoBQREbp2gg4l8d7H3vzTx8F3KsGCxys8pGz/n0lv+G9XOg==" />
   <p:extLst>
      <p:ext uri="{EFAFB233-063F-42B5-8137-9DF3F51BA10A}">
         <p15:sldGuideLst xmlns:p15="http://schemas.microsoft.com/office/powerpoint/2012/main">
            <p15:guide id="1" orient="horz" pos="2160">
               <p15:clr>
                  <a:srgbClr val="A4A3A4" />
               </p15:clr>
            </p15:guide>
            <p15:guide id="2" pos="2880">
               <p15:clr>
                  <a:srgbClr val="A4A3A4" />
               </p15:clr>
            </p15:guide>
         </p15:sldGuideLst>
      </p:ext>
      <p:ext uri="{2D200454-40CA-4A62-9FC3-DE9A4176ACB9}">
         <p15:notesGuideLst xmlns:p15="http://schemas.microsoft.com/office/powerpoint/2012/main" />
      </p:ext>
   </p:extLst>
</p:presentation>

This is an approach i found by asking on r/regex on redit. This is the regex i need to use:

regex = r"<p:modifyVerifier[^>]+>"

Hence the code will be

import re
file = open("my_file.xml", "r")
s =file.read()
file.close()
new_s = re.sub(r"<p:modifyVerifier[^>]+>", '',s)
file1 = open("new_file.xml", "w+")
file1.write(new_s)
file1.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM