动态 版块 发帖 消息 我的
Loading...
小绿叶技术博客
小绿叶技术博客
小绿叶技术Lv153   
python 修改文件多个位置 完整代码案例     


import os

def modify_file(file_path, replacements):
    with open(file_path, 'r') as file:
        content = file.read()

    for old_value, new_value in replacements.items():
        content = content.replace(old_value, new_value)

    with open(file_path, 'w') as file:
        file.write(content)


# 定义需要替换的内容和对应的新值
replacements = {
    'old_value1': 'new_value1',
    'old_value2': 'new_value2',
    'old_value3': 'new_value3'
}

# 指定文件路径
file_path = 'path/to/your/file.txt'

# 调用函数修改文件
modify_file(file_path, replacements)






 1  已被阅读了183次  楼主 2023-08-02 11:39:09
回复列表

回复:python 修改文件多个位置 完整代码案例

guest
登录之后更精彩~
Powered by HadSky 7.12.10
© 2015-2023 PuYueTian
您的IP:8.219.134.200,2023-10-04 01:23:05,Processed in 0.03276 second(s).
Powered by HadSky
小绿叶技术博客