首页 > 代码库 > Python修改文本内容
Python修改文本内容
将第一行内容改为第二行的内容格式
REPLACE INTO `LogCenterDbName` VALUES (29, 1, ‘lt4_his‘);
REPLACE INTO `LogCenterDbName` VALUES (29, 1, ‘loghis_pool1‘,‘pjbhislt4‘);
原文件
cat file.txt
REPLACE INTO `LogCenterDbName` VALUES (14, 0, ‘dx1_log‘);
REPLACE INTO `LogCenterDbName` VALUES (14, 1, ‘dx1_his‘);
REPLACE INTO `LogCenterDbName` VALUES (18, 0, ‘dx2_log‘);
REPLACE INTO `LogCenterDbName` VALUES (18, 1, ‘dx2_his‘);
REPLACE INTO `LogCenterDbName` VALUES (19, 0, ‘dx3_log‘);
REPLACE INTO `LogCenterDbName` VALUES (19, 1, ‘dx3_his‘);
REPLACE INTO `LogCenterDbName` VALUES (21, 1, ‘dx4_his‘);
REPLACE INTO `LogCenterDbName` VALUES (21, 0, ‘dx4_log‘);
REPLACE INTO `LogCenterDbName` VALUES (25, 0, ‘dx5_log‘);
REPLACE INTO `LogCenterDbName` VALUES (25, 1, ‘dx5_his‘);
REPLACE INTO `LogCenterDbName` VALUES (15, 0, ‘lt1_log‘);
REPLACE INTO `LogCenterDbName` VALUES (15, 1, ‘lt1_his‘);
REPLACE INTO `LogCenterDbName` VALUES (20, 0, ‘lt2_log‘);
REPLACE INTO `LogCenterDbName` VALUES (20, 1, ‘lt2_his‘);
REPLACE INTO `LogCenterDbName` VALUES (23, 0, ‘lt3_log‘);
REPLACE INTO `LogCenterDbName` VALUES (23, 1, ‘lt3_his‘);
REPLACE INTO `LogCenterDbName` VALUES (17, 0, ‘360_log‘);
REPLACE INTO `LogCenterDbName` VALUES (17, 1, ‘360_his‘);
REPLACE INTO `LogCenterDbName` VALUES (22, 0, ‘179_log‘);
REPLACE INTO `LogCenterDbName` VALUES (22, 1, ‘179_his‘);
REPLACE INTO `LogCenterDbName` VALUES (24, 0, ‘yy_log‘);
REPLACE INTO `LogCenterDbName` VALUES (24, 1, ‘yy_his‘);
REPLACE INTO `LogCenterDbName` VALUES (26, 0, ‘9377_log‘);
REPLACE INTO `LogCenterDbName` VALUES (26, 1, ‘9377_his‘);
REPLACE INTO `LogCenterDbName` VALUES (28, 0, ‘dx6_log‘);
REPLACE INTO `LogCenterDbName` VALUES (28, 1, ‘dx6_his‘);
REPLACE INTO `LogCenterDbName` VALUES (29, 0, ‘lt4_log‘);
REPLACE INTO `LogCenterDbName` VALUES (29, 1, ‘lt4_his‘);
脚本内容
#!/usr/bin/env python #coding:utf-8 f=open(‘file.txt‘,‘r‘) b=file(‘test.txt‘,‘a‘) for i in f: i=i.split("‘") q=i[1] q=q.split(‘_‘) zuo=q[0] you=q[1] format="‘pjb%s%s‘" % (you,zuo) nr=str(i[0]+‘loghis_pool1 ,‘+format+i[2],) b.write(nr) f.close() b.close()
修改后的文件内容
cat test.txt
REPLACE INTO `LogCenterDbName` VALUES (14, 0, loghis_pool1 ,‘pjblogdx1‘); REPLACE INTO `LogCenterDbName` VALUES (14, 1, loghis_pool1 ,‘pjbhisdx1‘); REPLACE INTO `LogCenterDbName` VALUES (18, 0, loghis_pool1 ,‘pjblogdx2‘); REPLACE INTO `LogCenterDbName` VALUES (18, 1, loghis_pool1 ,‘pjbhisdx2‘); REPLACE INTO `LogCenterDbName` VALUES (19, 0, loghis_pool1 ,‘pjblogdx3‘); REPLACE INTO `LogCenterDbName` VALUES (19, 1, loghis_pool1 ,‘pjbhisdx3‘); REPLACE INTO `LogCenterDbName` VALUES (21, 1, loghis_pool1 ,‘pjbhisdx4‘); REPLACE INTO `LogCenterDbName` VALUES (21, 0, loghis_pool1 ,‘pjblogdx4‘); REPLACE INTO `LogCenterDbName` VALUES (25, 0, loghis_pool1 ,‘pjblogdx5‘); REPLACE INTO `LogCenterDbName` VALUES (25, 1, loghis_pool1 ,‘pjbhisdx5‘); REPLACE INTO `LogCenterDbName` VALUES (15, 0, loghis_pool1 ,‘pjbloglt1‘); REPLACE INTO `LogCenterDbName` VALUES (15, 1, loghis_pool1 ,‘pjbhislt1‘); REPLACE INTO `LogCenterDbName` VALUES (20, 0, loghis_pool1 ,‘pjbloglt2‘); REPLACE INTO `LogCenterDbName` VALUES (20, 1, loghis_pool1 ,‘pjbhislt2‘); REPLACE INTO `LogCenterDbName` VALUES (23, 0, loghis_pool1 ,‘pjbloglt3‘); REPLACE INTO `LogCenterDbName` VALUES (23, 1, loghis_pool1 ,‘pjbhislt3‘); REPLACE INTO `LogCenterDbName` VALUES (17, 0, loghis_pool1 ,‘pjblog360‘); REPLACE INTO `LogCenterDbName` VALUES (17, 1, loghis_pool1 ,‘pjbhis360‘); REPLACE INTO `LogCenterDbName` VALUES (22, 0, loghis_pool1 ,‘pjblog179‘); REPLACE INTO `LogCenterDbName` VALUES (22, 1, loghis_pool1 ,‘pjbhis179‘); REPLACE INTO `LogCenterDbName` VALUES (24, 0, loghis_pool1 ,‘pjblogyy‘); REPLACE INTO `LogCenterDbName` VALUES (24, 1, loghis_pool1 ,‘pjbhisyy‘); REPLACE INTO `LogCenterDbName` VALUES (26, 0, loghis_pool1 ,‘pjblog9377‘); REPLACE INTO `LogCenterDbName` VALUES (26, 1, loghis_pool1 ,‘pjbhis9377‘); REPLACE INTO `LogCenterDbName` VALUES (28, 0, loghis_pool1 ,‘pjblogdx6‘); REPLACE INTO `LogCenterDbName` VALUES (28, 1, loghis_pool1 ,‘pjbhisdx6‘); REPLACE INTO `LogCenterDbName` VALUES (29, 0, loghis_pool1 ,‘pjbloglt4‘); REPLACE INTO `LogCenterDbName` VALUES (29, 1, loghis_pool1 ,‘pjbhislt4‘);
本文出自 “YHT的运维笔记” 博客,请务必保留此出处http://yht1990.blog.51cto.com/9014030/1937291
Python修改文本内容