【问题标题】:How do I edit text between 2 string delimiters in Python when the string is many lines当字符串多行时,如何在 Python 中的 2 个字符串分隔符之间编辑文本
【发布时间】:2021-09-18 00:28:51
【问题描述】:

我正在将 XML 文档转换为 .ckl 文档。它们是相似的文件格式,但并不是那么简单。我的大部分工作都在工作,但有一部分我被困住了。

在使用 ElementTree 解析 XML 之前,我必须将一些 <> 转换为 <>,因为原始 XML 有一些错误,需要更正才能正确解析。我没有意识到的一件事是,在某些组中,我需要离开 <>,因为 .ckl 阅读器程序将该文本显示为 <>

基本上,我纠正过度以便能够解析,但当它们在 <fixtext> 组中时需要将它们改回来。

为了进行初始更正,我将整个 XML 文件作为一个大字符串复制到一个变量中并执行 data.replace('<', '<') 这工作正常并替换了所有所需的实例,但它也更正了我需要离开的情况<

在此之后,我需要在解析之前将 <fixtext> 组中的那几个案例改回来,否则一切都会搞砸

TL;DR 我需要在行数发生变化的多行字符串中替换分隔符 <fixtest *tags here*></fixtext> 之间的 <>

任何帮助将不胜感激。如果您需要更多信息,请告诉我,我很乐意回答任何问题

原始 XML 关闭的示例:

<description>&lt;VulnDiscussion&gt;

这里,VulnDiscussion 应该是一个新标签

开始修复文本:

<fixtext fixref="F-22407r554595_fix">Configure the policy value for Computer Configuration &gt;&gt;
                Administrative Templates &gt;&gt; Windows Components &gt;&gt; BitLocker Drive Encryption &gt;&gt;
                Operating System Drives "Require additional authentication at startup" to "Enabled" with "Configure TPM
                Startup PIN:" set to "Require startup PIN with TPM" or with "Configure TPM startup key and PIN:" set to
                "Require startup key and PIN with TPM".
</fixtext>

【问题讨论】:

  • 您能否提供一个起始 XML 字符串的示例?
  • 这就够了?文档中有多个 fixtext 实例,但这是一个示例 @DarrylG
  • @JackMcGowan——可能够了。请求,因为最好为潜在的响应者提供示例文本以测试他们的答案。

标签: python xml replace elementtree


【解决方案1】:

使用正则表达式

import re
import html # In Python 3.2 a new html module was introduced, which is used for escaping reserved characters from HTML markup

# Example html Text with &lt; and &gt; between and outside tags
html_doc = '''&gt;&gt;&lt;&lt;&lt;&gt;&gt;&lt;&lt;&lt;blahblah<fixtext fixref="F-22407r554595_fix">Configure the policy value for Computer Configuration &gt;&gt;
                Administrative Templates &gt;&gt; Windows Components &gt;&gt; BitLocker Drive Encryption &gt;&gt;
                Operating System Drives "Require additional authentication at startup" to "Enabled" with "Configure TPM
                Startup PIN:" set to "Require startup PIN with TPM" or with "Configure TPM startup key and PIN:" set to
                "Require startup key and PIN with TPM".
</fixtext>&gt;&gt;&lt;&lt;&lt;blahblah'''


# Generate text with substitutions OP wants to reverse later on all the text
html_doc = html_doc.replace('&gt;', '>').replace('&lt;', '<')

# Regex pattern for detecting charcters between tags
p = re.compile(r"(?P<TAG_START><fixtext[^>]*>)(?P<TEXT>.*?)(?P<TAG_END></fixtext>)", flags = re.DOTALL)
indexes = p.groupindex     # groupindex on a compiled regular expression which prints the groups and their orders in the pattern string
                           # i.e. mappingproxy({'TAG_START': 1, 'TEXT': 2, 'TAG_END': 3}
    
# Only escape characters between tags (DOTALL flag for multiline)
corrected = re.sub(pattern, 
                   lambda m: m.group(indexes["TAG_START"]) + html.escape(m.group(indexes["TEXT"])) + m.group(indexes["TAG_END"]), 
                    html_doc) 
print(corrected)

注释已更正,仅在标签之间替换了

>><<<>><<<blahblah<fixtext fixref="F-22407r554595_fix">Configure the policy value for Computer Configuration &gt;&gt;
                Administrative Templates &gt;&gt; Windows Components &gt;&gt; BitLocker Drive Encryption &gt;&gt;
                Operating System Drives &quot;Require additional authentication at startup&quot; to &quot;Enabled&quot; with &quot;Configure TPM
                Startup PIN:&quot; set to &quot;Require startup PIN with TPM&quot; or with &quot;Configure TPM startup key and PIN:&quot; set to
                &quot;Require startup key and PIN with TPM&quot;.
</fixtext>>><<<blahblah

【讨论】:

    猜你喜欢
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2014-04-19
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多