[英]How do I insert a sentence into a string at a specific location?
Let's say there is a line like this:假设有这样一行:
<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>
Tell me how this can be done?告诉我如何做到这一点? I wanted to do
find ('.')
, But it won't work, since the dot is also in the link我想做
find ('.')
,但它不起作用,因为点也在链接中
<a href="//gifts.ru/catalog/model-futbolka-imperial"> Imperial 190 </a>
Expected output:预期输出:
<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>
There will be a lot of such insertions.会有很多这样的插入。
You can first find the index of the first .
你可以先找到第一个的索引
.
which occurs after the </a>
and append the text which you want to insert with the help of slicing.它发生在
</a>
之后,并在切片的帮助下附加要插入的文本。 However, this depends on the assumption that the main text is always coming after an anchor tag....然而,这取决于主要文本总是在锚标记之后的假设......
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)
You can adjust the spacing and/or fullstops which come before or after the inserted text as per your requirement by editing the slicing statement.您可以通过编辑切片语句来根据您的要求调整插入文本之前或之后的间距和/或句号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.