51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

python 读取文件 查找 替换

python 读取文件 查找 替换

Python 文件查找和替换功能文件内容的读取、查找特定文本

def tihuan_str(search_text,replace_text):
    search_text='中文在线'
    replace_text='中文在线-22233'

    path='/app/stock/stock/jishu_stock/zGetStockCode/TiCaiKu/ticat1.txt'

    # 打开文件并读取内容
    # with open(path, "r", encoding="utf-8") as file:
    with open(path, "r") as file:
        content = file.read()

    # 使用 replace() 方法替换文本
    new_content = content.replace(search_text, replace_text)

    # 将新内容写回文件
    with open(path, "w") as file:
    # with open(path, "w", encoding="utf-8") as file:
        file.write(new_content)

if __name__ == '__main__':
    search_text='中文在线'
    replace_text='中文在线-22233'
    tihuan_str(search_text, replace_text)
赞(6)
未经允许不得转载:工具盒子 » python 读取文件 查找 替换