您的位置 首页 python

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)

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章