Remove leading and trailing spaces?

后端 未结 4 1433
醉梦人生
醉梦人生 2020-12-07 19:57

I\'m having a hard time trying to use .strip with the following line of code.

Thanks for the help.

f.write(re.split(\"Tech ID:|Name:|Account #:\",li         


        
4条回答
  •  被撕碎了的回忆
    2020-12-07 20:22

    Starting file:

         line 1
       line 2
    line 3  
          line 4 
    

    Code:

    with open("filename.txt", "r") as f:
        lines = f.readlines()
        for line in lines:
            stripped = line.strip()
            print(stripped)
    

    Output:

    line 1
    line 2
    line 3
    line 4
    

提交回复
热议问题