如何使用python将小于号和大于号转换为父标签内的实体符号?

2024-05-20 14:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难在<>父标记内编写函数代码来转换\&lt;\&gt;。原来的代码是这样的:

<xml>
<body>
<month>
<sep>Hello world!<p>This is 
september!</p> Hello world!<b>And today's Firday!</b></sep>
</month>
<month>
<sep><i>This is October!<i></sep>
</month>
</body>
</xml>

结果应该是:

<xml>
<body>
<month>
<sep>Hello world!\&lt;p\&gt;This is 
september!\&lt;/p\&gt; Hello world!\&lt;b\&gt;And today's Firday!\&lt;/b\&gt;</sep>
</month>
<month>
<sep>\&lt;i\&gt;This is October!\&lt;i\&gt;</sep>
</month>
</body>
</xml>

到目前为止,我的代码是这样的:

text1 = re.findall(r"<sep>((.|\n)*?)<\/sep>", f.read())
text2 = re.sub(r"<(.*?)>", r"\&lt;"+r"\1"+"\&gt;", text1)

但是我如何把转换后的文本放回原始文件呢? 谢谢!你知道吗


Tags: and代码ltgthelloworldtodayis
1条回答
网友
1楼 · 发布于 2024-05-20 14:38:22
sample = """<xml>
<body>
<month>
<sep>Hello world!<p>This is 
september!</p> Hello world!<b>And today's Firday!</b></sep>
</month>
<month>
<sep><i>This is October!<i></sep>
</month>
</body>
</xml>"""

def encode_text(in_txt):
  out_txt = copy.copy(in_txt)
  matches = re.findall(r"<sep>((.|\n)*?)<\/sep>", in_txt)
  for (txt,_) in matches:
    out_txt = out_txt.replace(txt, re.sub(r"<(.*?)>", r"\&lt;"+r"\1"+"\&gt;", txt), 1)
  return out_txt

def decode_text(in_txt):
  out_txt = copy.copy(in_txt)
  matches = re.findall(r"<sep>((.|\n)*?)<\/sep>", in_txt)
  for (txt,_) in matches:
    out_txt = out_txt.replace(txt, re.sub(r"\\\&lt;(.*?)\\\&gt;", r"<\1>", txt), 1)
  return out_txt

result_encoded = encode_text(sample)
result_decoded = decode_text(result_encoded)

print(result_encoded)打印:

<xml>
<body>
<month>
<sep>Hello world!\&lt;p\&gt;This is 
september!\&lt;/p\&gt; Hello world!\&lt;b\&gt;And today's Firday!\&lt;/b\&gt;</sep>
</month>
<month>
<sep>\&lt;i\&gt;This is October!\&lt;i\&gt;</sep>
</month>
</body>
</xml>

print(result_decoded)打印时:

<xml>
<body>
<month>
<sep>Hello world!<p>This is 
september!</p> Hello world!<b>And today's Firday!</b></sep>
</month>
<month>
<sep><i>This is October!<i></sep>
</month>
</body>
</xml>

另外,请注意:

result_decode == sample
Out[87]: True

相关问题 更多 >