我有一个文本文件(“input.param”),它用作包的输入文件。我需要修改一个参数的值。需要更改的行如下:
param1 0.01…
字符串是不可变对象,这意味着就地操作不起作用:
astring = '' def some_func(a): a = a + '1' some_func(astring) astring # '' # Same goes for str.replace otherstr = 'bcd' otherstr.replace('b', '\n') otherstr # 'bcd'
没有对函数中携带的对象的引用,因此它不会自行修改,这就是为什么需要将它分配给变量的原因:
astring = 'bcd' astring = astring.replace('b', '') astring # 'cd'