У меня есть следующий код.Python string.replace не заменяет
def render_markdown(markdown):
"Replaces markdown links with plain text"
# non greedy
# also includes images
RE_ANCHOR = re.compile(r"\[[^\[]*?\]\(.+?\)")
# create a mapping
mapping = load_mapping()
anchors = RE_ANCHOR.findall(markdown)
counter = -1
while len(anchors) != 0:
for link in anchors:
counter += 1
text, href = link.split('](')[:2]
text = '-=-' + text[1:] + '-=-'
text = text.replace(' ', '_') + '_' + str(counter)
href = href[: -1]
mapping[text] = href
markdown = markdown.replace(link, text)
anchors = RE_ANCHOR.findall(markdown)
return markdown, mapping
Однако функция уценки не заменяет все ссылки. Почти никто не заменен. Я посмотрел на SO и нашел много вопросов, относящихся к этому. Проблемы найдены были типа:
abc.replace(x, y) instead of abc = abc.replace(x, y)
Я делаю это, но строка не заменяются
Можете ли вы привести пример того, что вы ожидаете? – kindall
Почему вы не используете [многие библиотеки разметки] (https://pypi.python.org/pypi?%3Aaction=search&term=markdown&submit=search), доступные для установки, готовые к работе? –
@kindall Это попытка научить себя. Поэтому я строю с нуля. Я ожидаю, что [поиск] (https://google.com) будет заменен на - = - search - = - – theSage