我有一个使用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文件中的那些行/部分?
发布于 2018-04-09 08:34:25
使用正则表达式处理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>发布于 2018-04-09 16:10:56
对于输出,这似乎是可行的
(?mi)((?:password=|user=)[^\n]*$|\<property key=\"variableNames\"\/\>\n|\<property key=\"variableValues\"\/\>\n)Demo,,,其中将换行符\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)https://stackoverflow.com/questions/49723769
复制相似问题