在循环中重复替换字符串的一部分

2024-09-27 09:28:34 发布

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

我创建了一个HTML文本清理器,可以删除标记之间的数据。
它在一次迭代中工作得很好,但在一个循环中就不行了。你知道吗

问题是,由于Python的字符串不变性,我无法将newhtml保存为变量。
所以,我的循环只对函数return的最后一次迭代有效。你知道吗

在这种情况下,最好的做法是什么?你知道吗

def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += len(sub) # use start += 1 to find overlapping matches

def replace_string(index1, index2, mainstring):
    replacementstring = ''
    return mainstring.replace(mainstring[index1:index2], replacementstring)

def strip_images(html):
    begin_indexes = list(find_all(html, '<DESCRIPTION>GRAPHIC'))
    end_indexes = list(find_all(html, '</TEXT>'))
        for i in range(len(begin_indexes)):
            if begin_indexes[i] > end_indexes[i]:
                end_indexes.pop(0)
    else:
        if len(begin_indexes) == len(end_indexes):
            break

    for i in range(len(begin_indexes)):
        #code problem is here--
        newhtml = replace_string(begin_indexes[i],end_indexes[i], html)
        if i == len(begin_indexes) - 1:
            return newhtml
            #code only returns one iteration

var = strip_images(html)
print var

Tags: lenreturnifdefhtmlallfindstart
2条回答

开始工作了,下面是代码片段。它并不漂亮,但它的工作是删除这两个标记之间的文本:

def find_all(a_str, sub):
   start = 0
   while True:
    start = a_str.find(sub, start)
    if start == -1: return
    yield start
    start += len(sub) # use start += 1 to find overlapping matches

def strip_images(html):
begin_indexes = list(find_all(html, '<DESCRIPTION>GRAPHIC'))
end_indexes = list(find_all(html, '</TEXT>'))
for i in range(len(begin_indexes)):
    if begin_indexes[i] > end_indexes[i]:
        end_indexes.pop(0)
    else:
        if len(begin_indexes) == len(end_indexes):
            break

newhtml = html
begin_indexes2 = begin_indexes[::-1]
end_indexes2 = end_indexes[::-1]
for i in range(len(begin_indexes2)):
#for i, value in enumerate(begin_indexes,0):
    #end_indexes.reset_index(drop=True)
    newhtml = list(newhtml)
    del newhtml[begin_indexes2[i]:end_indexes2[i]]

    if i == len(begin_indexes2) - 1:
        str1 = ''.join(newhtml)
        return str1

您当前的问题是html在循环中从不改变。因此,无论列表的长度如何,您的输入总是第一次迭代。你知道吗

这里的解决方案遵循以下步骤

  • 将字符串赋给循环之前的原始值
  • 在循环中编辑,传入当前内容,返回替换的字符串
  • 循环后从函数返回

newhtml = html 
for begin, end in zip(begin_indexes, end_indexes):
    newhtml = replace_string(begin, end, newhtml)
return newhtml

相关问题 更多 >

    热门问题