把内容: 192.168.0.1 /data/zabbix /usr/local/zabbix /data/zabbix/zabbix_log 192.168.0.2 /data/mysql /usr/local/mysql /data/mysql/xxl_job 192.168.0.3 /data/oracle /usr/local/oracle /data/oracle/xxl_job 替换成: 192.168.0.1 /data/zabbix /usr/local/zabbix/data/zabbix/zabbix_log 192.168.0.2 /data/mysql /usr/local/mysql /data/mysql/xxl_job 192.168.0.3 /data/oracle /usr/local/oracle/data/oracle/xxl_job 脚本: import re def read_file(from_log,to_log): compile_rule = re.compile(r'd+[.]d+[.]d+[.]d+') with open(from_log) as read_f,open(to_log,'w') as write_f: for content in read_f: if re.findall(compile_rule, content) : write_f.write( ' '+ content.strip(' ') ) else: write_f.write(content.strip(' ') +' ' ) read_file('1.log','2.log')