Re.sub not working for me

前端 未结 2 1969
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 08:47

I\'m trying to get re.sub to replace a pattern specified with a value for example

for lines in f:
    pattern=\'\\${2}\'+key[0]+\'\\${2}\'
    r         


        
2条回答
  •  忘掉有多难
    2020-11-27 08:57

    You are assigning the result of re.sub back to a variable, right? e.g.

    lines = re.sub(pattern, key[1], lines)
    

    It's a string, so it can't be changed (strings are immutable in Python), therefore a new string is created and returned to you. If you don't assign it back to a name, you will lose it.

提交回复
热议问题