red_alert_3y
2021-08-10 16:05
采纳率: 100%
浏览 36
已结题

python的replace函数用起来没效果怎么办?

file_path = '/red_alert_3y/python_work/file_txt_csv/python_learning_notebook.txt'

with open(file_path, encoding='UTF-8') as file:
    lines = file.readlines()
    for i in lines:
        i.replace('python', 'C++')
        print("\t" + i.rstrip().title())

然后输出是这样的

C:\Users\PZH71P\AppData\Local\Programs\Python\Python38\python.exe D:/red_alert_3y/python_work/python_file_py/file_python_learning_notebook.py
    In Python :The Zen Of Python, By Tim Peters
    In Python :Beautiful Is Better Than Ugly.
    In Python :Explicit Is Better Than Implicit.
    In Python :Simple Is Better Than Complex.
    In Python :Complex Is Better Than Complicated.
    In Python :Flat Is Better Than Nested.
    In Python :Sparse Is Better Than Dense.
    In Python :Readability Counts.
    In Python :Special Cases Aren'T Special Enough To Break The Rules.
    In Python :Although Practicality Beats Purity.
    In Python :Errors Should Never Pass Silently.
    In Python :Unless Licitly Silenced.
    In Python :In The Face Of Ambiguity, Refuse The Temptation To Guess.
    In Python :There Should Be One-- And Preferably Only One --Obvious Way To Do It.
    In Python :Although That Way May Not Be Obvious At First Unless You'Re Dutch.
    In Python :Now Is Better Than Never.
    In Python :Although Never Is Often Better Than *Right* Now.
    In Python :If The Implementation Is Hard To Explain, It'S A Bad Idea.
    In Python :If The Implementation Is Easy To Explain, It May Be A Good Idea.
    In Python :In Python :Namespaces Are One Honking Great Idea -- Let'S Do More Of Those!
Process finished with exit code 0

我的‘file_path’里面是我的改版的python之禅,前面加了In python: ,
但是用replace函数改Python为C++却改不了
求同志改改

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 幸福丶如此 2021-08-10 16:21
    已采纳
    
    file_path = '/red_alert_3y/python_work/file_txt_csv/python_learning_notebook.txt'
    with open(file_path, encoding='UTF-8') as file:
        lines = file.readlines()
        for i in lines:
            i = i.replace('python', 'C++')   #替换好了以后一定要赋值
            print("\t" + i.rstrip().title())
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题