How to properly iterate with re.sub() in Python

后端 未结 3 1555
北荒
北荒 2021-02-14 12:36

I want to make a Python script that creates footnotes. The idea is to find all strings of the sort \"Some body text.{^}{Some footnote text.}\" and replace them with

3条回答
  •  臣服心动
    2021-02-14 13:01

    A variation and Python-3-only solution:

    def make_create_footnote_numbers(start=1):
        count = start - 1
        def create_footnote_numbers(match):
            nonlocal count
            count += 1
            return "{}".format(count)
        return create_footnote_numbers
    
    new_body_text = re.sub(pattern, make_create_footnote_numbers(), text)
    

提交回复
热议问题