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

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条回答 默认 最新

  • 7*24 工作者 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条)

报告相同问题?

问题事件

  • 系统已结题 8月19日
  • 已采纳回答 8月11日
  • 创建了问题 8月10日

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题