如何摆脱一行中的“ +”?

时间:2019-07-29 06:17:31

标签: python string

我在下面有txt文件,如果数字前面有+,我希望删除“ +”。

  

+905061459318

     

+905458507534

     

+905437335094

我已经尝试了Stackoverflow中几乎所有存在的解决方案,但是仍然无法从行中删除+。

代码在这里。

with open("numbers.txt") as f:
lines = [line.rstrip('\n') for line in open("numbers.txt")]
for line in lines:
    if line.startswith("+"):
        line.replace("+","")
    else:
        pass

1 个答案:

答案 0 :(得分:2)

您已经有line.rstrip('\n')。设为line.rstrip('\n').lstrip('+')