简体   繁体   English

从python文件中删除字符串部分,动态字符串匹配?

[英]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.嗨,我有一个每次动态生成的大 xml(见最后)文件。 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:所以我想要做的是删除行的特定部分,但请参见以下示例:在大 xml 文件中,我需要通过 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!但是这里 hashData 和 saltData 每次都会改变,所以硬编码的字符串匹配方法不起作用!

<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.注意在</p:defaultTextStyle> <p:modifyVerifier之前没有空格的</p:defaultTextStyle>我需要</p:defaultTextStyle>并删除上面提到的部分,然后还需要代码。

So how do i first match a string with the following section with dynamically changing string in python!那么我如何首先将字符串与以下部分匹配,并在 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.以及如何在从 xml 文件 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.这是我通过在 redit 上询问 r/regex 发现的方法。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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