添加一个新行我有以下文字:应用re.sub在python
xml = '''
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<crew_member billing="top"
<display_name>John Viscount</display_name>
</crew_member>
<products>
<territory>GB</territory>
</products>'''
我需要删除以下<crew_member>
块。这是我目前在做什么:
clean_xml = re.sub('<crew_member>.*</crew_member>', '', metadata_contents,
flags=re.DOTALL)
但是,还加入了新行:
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<products>
<territory>GB</territory>
</products>
我将如何改变正则表达式来剥离换行符一样,所以它看起来像:
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<products>
<territory>GB</territory>
</products>'
只需将'\ n'添加到匹配字符串的末尾。 – Anorov
这不是它添加了一个换行符,而是你没有删除它。 – saulspatz