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

时间:2016-09-30 20:00:20

标签: python string parsing concatenation

我正在尝试搜索我的字符串然后连接字符串。

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”旁边的字符串。

另外我的意思是继续添加字符串而不是替换是例如。 我有一个 - 字符串“concatenate1”中的第一个用户类型 - 字符串“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>

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以使用+符号连接字符串。

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

答案 2 :(得分:0)

要在字符串变量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>'