如果找到字符串,则连接到字符串旁边

2024-04-30 05:42:45 发布

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

我试图搜索我的字符串,然后连接字符串。你知道吗

string =    ' <html><body><svg style="background: #40484b;" version="1.1" viewbox="0 0 400 150" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs>
    <!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
    <rect height="100" id="path-1" width="100" x="25" y="25"></rect>
    <rect height="100" id="path-3" width="100" x="150" y="25"></rect>
    <rect height="100" id="path-5" width="100" x="275" y="25"></rect>
    <!-- gradient -->
    <lineargradient id="gradient" x1="50%" x2="50%" y1="0%" y2="100%">
    <stop offset="0%" stop-color="#FF8D77"></stop>
    <stop offset="50%" stop-color="#FFBDB1"></stop>
    <stop offset="100%" stop-color="#F4E3F6"></stop>
    </lineargradient>
    <!-- clip-path -->
    <clippath id="clip">
    <circle cx="325" cy="75" r="50"></circle>
    </clippath>
    <!-- filter -->
    </defs>>
    <g fill-rule="evenodd">
    <use fill="#FF8D77" fill-opacity="0.5" filter="url(#glow)" xlink:href="#path-1"></use>
    <use fill="url(#gradient)" xlink:href="#path-3"></use>
    <use clip-path="url(#clip)" fill="#FF8D77" xlink:href="#path-5"></use>
    </g>
    </svg>
    </body></html>'

我的绳子就是这样的。 到目前为止,这就是我所拥有的

    string_list = string
    if "<defs>" in string:
        ###I'm trying to concatenate strings after <defs> like <defs> string string

另外,我想继续附加字符串而不是替换。例如,如果我在某个对象中有一个用户类型,它会在defs之后不断添加字符串,而不是替换我已有的字符串。你知道吗

我期望的输出是这样的,我添加了“concatenated string1”和“concatenated string2”。我试图在我的字符串列表中的“defs”旁边添加字符串。你知道吗

另外,我所说的不断添加字符串而不是替换的意思是例如。 我有一个 -字符串“concatenae1”中的第一个用户类型 -字符串“concatenate2”中的第二个用户类型 我想在我的defs标签旁边添加两个concatenate 1+contatenate 2。你知道吗

<html><body><svg style="background: #40484b;" version="1.1" viewbox="0 0 400 150" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs> <concatenated string1> <concatenated string2>
<!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
<rect height="100" id="path-1" width="100" x="25" y="25"></rect>
<rect height="100" id="path-3" width="100" x="150" y="25"></rect>
<rect height="100" id="path-5" width="100" x="275" y="25"></rect>
<!-- gradient -->
<lineargradient id="gradient" x1="50%" x2="50%" y1="0%" y2="100%">
<stop offset="0%" stop-color="#FF8D77"></stop>
<stop offset="50%" stop-color="#FFBDB1"></stop>
<stop offset="100%" stop-color="#F4E3F6"></stop>
</lineargradient>
<!-- clip-path -->
<clippath id="clip">
<circle cx="325" cy="75" r="50"></circle>
</clippath>
<!-- filter -->
</defs>>
<g fill-rule="evenodd">
<use fill="#FF8D77" fill-opacity="0.5" filter="url(#glow)" xlink:href="#path-1"></use>
<use fill="url(#gradient)" xlink:href="#path-3"></use>
<use clip-path="url(#clip)" fill="#FF8D77" xlink:href="#path-5"></use>
</g>
</svg>
</body></html>

Tags: path字符串svgrectidstringclipuse
3条回答

要在字符串变量html中插入其他字符串,可以执行以下操作:

import re
html = '<html> ... <defs><  inserts will go here  > ... </html>'
insert = 'this line gets inserted'
html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + insert + r'\g<2>', html)

# html is now '<html> ... <defs>this line gets inserted<  inserts will go here  > ... </html>'

如果你把它变成一个函数,你可以不断重复注入更多的行。你知道吗

import re

def add_definitions(html, definitions):
  html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + ''.join(definitions) + r'\g<2>', html)
  return html

# then you can use it like this...
html = '<html> ... <defs><  inserts will go here  > ... </html>'
html = add_definitions(html, [ 'add this line 1.', 'add this line 2.' ])

# html should now be '<html> ... <defs>add this line 1.add this line2.<  inserts will go here  > ... </html>'

可以用+符号连接字符串。你知道吗

>>> 'foo' + 'bar'
'foobar'

对于包含多行的字符串,应该使用三重单引号或三重双引号。最好使用“for”循环,而不是“if”语句。你知道吗

相关问题 更多 >