繁体   English   中英

如何在特定位置的字符串中插入句子?

[英]How do I insert a sentence into a string at a specific location?

假设有这样一行:

<a href="//gifts.ru/catalog/model-futbolka-imperial"> Imperial 190 </a> is a premium model, decoration of any promotion and a worthy personal gift. Soft, comfortable tight T-shirt, with a collar protected from deformation. It holds its shape perfectly and is ideal for applying a logo. The absence of side seams allows you to apply the image almost over the entire surface. <br>

告诉我如何做到这一点? 我想做find ('.') ,但它不起作用,因为点也在链接中

<a href="//gifts.ru/catalog/model-futbolka-imperial"> Imperial 190 </a>

预期输出:

<a href="//gifts.ru/catalog/model-futbolka-imperial"> Imperial 190 </a> is a premium model, decoration of any promotion and a worthy personal gift. **[INSERTED TEXT]**. Soft, comfortable tight T-shirt, with a collar protected from deformation. It holds its shape perfectly and is ideal for applying a logo. The absence of side seams allows you to apply the image almost over the entire surface. <br>

会有很多这样的插入。

你可以先找到第一个的索引. 它发生在</a>之后,并在切片的帮助下附加要插入的文本。 然而,这取决于主要文本总是在锚标记之后的假设......

text = "Text to be inserted."
to_parse = '<a href="//gifts.ru/catalog/model-futbolka-imperial"> Imperial 190 </a> is a premium model, decoration of any promotion and a worthy personal gift. Soft, comfortable tight T-shirt, with a collar protected from deformation. It holds its shape perfectly and is ideal for applying a logo. The absence of side seams allows you to apply the image almost over the entire surface. <br>'

index = to_parse.find(".", to_parse.find("</a>"))
to_parse = to_parse[:index+1] + text + to_parse[index+1:]
print(to_parse)

您可以通过编辑切片语句来根据您的要求调整插入文本之前或之后的间距和/或句号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM