首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中删除文件中的特定行

在python中删除文件中的特定行
EN

Stack Overflow用户
提问于 2018-04-09 07:23:15
回答 2查看 52关注 0票数 1

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

代码语言:javascript
复制
    <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来删除密码和用户部分以及变量部分。我使用以下代码:

代码语言:javascript
复制
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)

这是可行的,但并不完全像我想要的那样,它删除了部分,但留下了空行,例如:

代码语言:javascript
复制
 <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文件中的那些行/部分?

EN

回答 2

Stack Overflow用户

发布于 2018-04-09 08:34:25

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

代码语言:javascript
复制
<?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>
票数 2
EN

Stack Overflow用户

发布于 2018-04-09 16:10:56

对于输出,这似乎是可行的

代码语言:javascript
复制
(?mi)((?:password=|user=)[^\n]*$|\<property key=\"variableNames\"\/\>\n|\<property key=\"variableValues\"\/\>\n)

Demo,,,其中将换行符\n添加到正则表达式的某些部分,以便avoid creating empty line

在python中,可能是这样的。

代码语言:javascript
复制
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,那么可以尝试使用这个正则表达式来匹配文本中的空行。

代码语言:javascript
复制
(?m)^\s*$\n

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

代码语言:javascript
复制
newText = re.sub(r'(?m)^\s*$\n','',newText)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49723769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档