Delte Specifc lines from file in python



我有一个使用 xml 模式的文件。它看起来像这样:

    <maplayer simplifyAlgorithm="0" minimumScale="0" maximumScale="2500" simplifyDrawingHints="0" readOnly="0" minLabelScale="0" maxLabelScale="1e+08" simplifyDrawingTol="1" geometry="Point" simplifyMaxScale="1" type="vector" hasScaleBasedVisibilityFlag="1" simplifyLocal="1" scaleBasedLabelVisibilityFlag="0">
      <id></id>
      <datasource>port=1521 user=test_user password=test_passwd</datasource>
      <keywordList>
        <value></value>
      </keywordList>
      <featformsuppress>0</featformsuppress>
      <editorlayout>generatedlayout</editorlayout>
      <widgets/>
      <conditionalstyles>
        <rowstyles/>
        <fieldstyles/>
      </conditionalstyles>
    </maplayer>
  </projectlayers>
  <properties>
    <Variables>
      <variableNames type="QStringList">
        <value>paswd</value>
        <value>user</value>
      </variableNames>
      <variableValues type="QStringList">
        <value>5zdgf</value>
        <value>dgdgdgfdg</value>
      </variableValues>
      </Variables>
    <customproperties>
    <property key="labeling/textColorR" value="0"/>
    <property key="labeling/textTransp" value="0"/>
    <property key="labeling/upsidedownLabels" value="0"/>
    <property key="labeling/useSubstitutions" value="false"/>
    <property key="labeling/wrapChar" value=""/>
    <property key="labeling/xOffset" value="0"/>
    <property key="labeling/yOffset" value="0"/>
    <property key="labeling/zIndex" value="0"/>
    <property key="variableNames"/>
    <property key="variableValues"/>
  </customproperties>

所以我想用python来删除密码和用户部分以及变量部分。我使用以下代码:

import re
with open('C:myfile.txt') as oldfile, open('C:myfile_withoutPW.txt', 'w') as newfile:
    oldText = oldfile.read()
    noPass = re.sub(r'(password=).*?(?=s) ', '', oldText.rstrip())
    noPass_noUser = re.sub(r'(user=).*?(?=s) ', '', noPass.rstrip())
    # fehlt noch
    newText = re.sub(re.escape(r'<property key="variableNames"/>'), '', noPass_noUser.rstrip())
    newText = re.sub(re.escape(r'<property key="variableValues"/>'), '', newText.rstrip())
    newfile.write(newText)

这有效,但并不完全如我所愿,它删除了部分,但它留下了空行,例如:

 <property key="labeling/wrapChar" value=""/>
        <property key="labeling/xOffset" value="0"/>
        <property key="labeling/yOffset" value="0"/>
        <property key="labeling/zIndex" value="0"/>

      </customproperties>
      <blendMode>0</blendMode>
      <featureBlendMo

我如何解决这个问题以完全删除这些行/部分形成我的 txt 文件?

使用正则表达式处理 xml 是有风险的。假设属性元素位于多行上。另一种方法是使用可扩展样式表转换 (XSLT(。我不知道您的所有要求,因此尝试匹配您的示例:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- pretty print output -->  
  <xsl:strip-space elements="*" />
  <xsl:output method="xml" indent="yes"/>
  <!-- strip unwanted elements and attributes -->  
  <xsl:template match="datasource|Variables|@user|@password"/>
  <!-- pass everything else through -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <!-- start tranform at the root -->
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

这似乎适用于输出

(?mi)((?:password=|user=)[^n]*$|<property key="variableNames"/>n|<property key="variableValues"/>n)

演示,,,其中换行符n被添加到正则表达式的某些部分,以便avoid creating empty line .

在python中,可能是这样的

ss=""" copy&paste your string in this area """
regx= re.compile(r'(?mi)((?:password=|user=)[^n]*$|<property key="variableNames"/>n|<property key="variableValues"/>n)')
print(regx.sub('',ss))

如果您希望通过删除匹配的字符串来创建remove empty lines那么您可以尝试使用此正则表达式来匹配文本中的空行。

(?m)^s*$n

因此,它适用于通过插入此行的脚本。

newText = re.sub(r'(?m)^s*$n','',newText)

最新更新