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日

悬赏问题

  • ¥15 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动