免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 4730 | 回复: 23
打印 上一主题 下一主题

更改配置文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-11 11:25 |只看该作者 |倒序浏览
下面是一段日志分析的配置文件,  现在我想把StartTime=后面的时间全替换成1204144200 .
不知道怎么样能实现, 单纯用string.replace()无法实现了, 望各位高手指教!  多谢

  1. [Common]
  2. RunUnderCustomAccount=0
  3. AccountName=
  4. AccountPassword=
  5. TaskCount=106
  6. [Task0]
  7. Name=0i.3g.cn
  8. Enabled=1
  9. StartTime=1204144200
  10. RepeatPeriodCount=1
  11. RepeatPeriodType=2
  12. Priority=0
  13. Command=i.3g.cn
  14. Action=0
  15. UID=VZXRZSIVJXPUF
  16. UsePreCommand=0
  17. PreCommand=
  18. PreCommandParams=
  19. PreCommandWorkDir=
  20. UsePostCommand=0
  21. PostCommand=
  22. PostCommandParams=
  23. PostCommandWorkDir=
  24. [Task1]
  25. Name=0i.3g.cn-pv
  26. Enabled=1
  27. StartTime=1204144200
  28. RepeatPeriodCount=1
  29. RepeatPeriodType=2
  30. Priority=0
  31. Command=i.3g.cn-pv
  32. Action=0
  33. UID=XCI4JQZLNOJPW
  34. UsePreCommand=0
  35. PreCommand=
  36. PreCommandParams=
  37. PreCommandWorkDir=
  38. UsePostCommand=0
  39. PostCommand=
  40. PostCommandParams=
  41. PostCommandWorkDir=
  42. [Task2]
  43. Name=0mail.3g.net.cn
  44. Enabled=1
  45. StartTime=1204145400
  46. RepeatPeriodCount=1
  47. RepeatPeriodType=2
  48. Priority=0
  49. Command=mail.3g.cn
  50. Action=0
  51. UID=MB66XV8PM7HIF
  52. UsePreCommand=0
  53. PreCommand=
  54. PreCommandParams=
  55. PreCommandWorkDir=
  56. UsePostCommand=0
  57. PostCommand=
  58. PostCommandParams=
  59. PostCommandWorkDir=
  60. [Task3]
  61. Name=0softdown.3g.net.cn
  62. Enabled=1
  63. StartTime=1204145400
  64. RepeatPeriodCount=1
  65. RepeatPeriodType=2
  66. Priority=0
  67. Command=softdown.3g.net.cn
  68. Action=0
  69. UID=PDPX8CTF32Q2I
  70. UsePreCommand=0
  71. PreCommand=
  72. PreCommandParams=
  73. PreCommandWorkDir=
  74. UsePostCommand=0
  75. PostCommand=
  76. PostCommandParams=
  77. PostCommandWorkDir=
  78. [Task4]
  79. Name=0ss.3g.cn
  80. Enabled=1
  81. StartTime=1206651000
  82. RepeatPeriodCount=1
  83. RepeatPeriodType=2
  84. Priority=0
  85. Command=ss.3g.cn
  86. Action=0
  87. UID=8PN8LAPQ1XDRZ
  88. UsePreCommand=0
  89. PreCommand=
  90. PreCommandParams=
  91. PreCommandWorkDir=
  92. UsePostCommand=0
  93. PostCommand=
  94. PostCommandParams=
  95. PostCommandWorkDir=
  96. [Task5]
  97. Name=0ss.3g.cn-pv
  98. Enabled=1
  99. StartTime=1207602000
  100. RepeatPeriodCount=1
  101. RepeatPeriodType=2
  102. Priority=0
  103. Command=ss.3g.cn-pv
  104. Action=0
  105. UID=899LOTPXDE4FB
  106. UsePreCommand=0
  107. PreCommand=
  108. PreCommandParams=
  109. PreCommandWorkDir=
  110. UsePostCommand=0
  111. PostCommand=
  112. PostCommandParams=
  113. PostCommandWorkDir=
  114. [Task6]
  115. Name=0vod.3g.cn
  116. Enabled=1
  117. StartTime=1204146000
  118. RepeatPeriodCount=1
  119. RepeatPeriodType=2
  120. Priority=0
  121. Command=vod.3g.cn
  122. Action=0
  123. UID=S2OIKH8Q5H2L8
  124. UsePreCommand=0
  125. PreCommand=
  126. PreCommandParams=
  127. PreCommandWorkDir=
  128. UsePostCommand=0
  129. PostCommand=
  130. PostCommandParams=
  131. PostCommandWorkDir=
  132. [Task7]
  133. Name=1book1
  134. Enabled=1
  135. StartTime=1179181200
  136. RepeatPeriodCount=1
  137. RepeatPeriodType=2
  138. Priority=0
  139. Command=1book1.3g.net.cn
  140. Action=0
  141. UID=GN9EBPBXIBRHS
  142. UsePreCommand=0
  143. PreCommand=
  144. PreCommandParams=
  145. PreCommandWorkDir=
  146. UsePostCommand=0
  147. PostCommand=
  148. PostCommandParams=
  149. PostCommandWorkDir=
  150. [Task8]
  151. Name=1book2
  152. Enabled=1
  153. StartTime=1179181200
  154. RepeatPeriodCount=1
  155. RepeatPeriodType=2
  156. Priority=0
  157. Command=1book2.3g.net.cn
  158. Action=0
  159. UID=GN9F1PBYIC3AP
  160. UsePreCommand=0
  161. PreCommand=
  162. PreCommandParams=
  163. PreCommandWorkDir=
  164. UsePostCommand=0
  165. PostCommand=
  166. PostCommandParams=
  167. PostCommandWorkDir=

复制代码

论坛徽章:
0
2 [报告]
发表于 2009-03-11 11:50 |只看该作者

回复 #1 caesarok 的帖子

1.为啥不用vim之类替换?

2.StartTime=1204144200
你读取文件时候,可以判断单行是否以“StartTime=”开头,开头则把这行改成“StartTime=xxxxxx”

3.再想麻烦点把就用正则,具体查看help(re.sub),写个表达式……

论坛徽章:
0
3 [报告]
发表于 2009-03-11 11:51 |只看该作者
a=123
a=str(a)
a=a.replace('1','one',1)
print  a
不知道可以不

论坛徽章:
0
4 [报告]
发表于 2009-03-11 11:54 |只看该作者

回复 #2 xiaoyu9805119 的帖子

文件太大,行数太多了,

论坛徽章:
0
5 [报告]
发表于 2009-03-11 12:19 |只看该作者
原帖由 caesarok 于 2009-3-11 11:54 发表
文件太大,行数太多了,

就按行读取文件把,一个个替换,然后重新写入文件

论坛徽章:
0
6 [报告]
发表于 2009-03-11 12:37 |只看该作者

回复 #5 xiaoyu9805119 的帖子

我觉得难点在于如何把 包含"StartTime="的行内容替换成"StartTime=1204144200"
问题是 :1 包含 "StartTime=" 的行带有StartTime=145454500等的不同时间,而string.replace(str1,str2,num=stringcount(str1)).
2  
更细的办法我还在想跟试
line.replace(line,'StartTime=1204144200')

[ 本帖最后由 caesarok 于 2009-3-11 13:02 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2009-03-11 13:02 |只看该作者
试出来了

  1. #-*-coding=gbk-*-
  2. import glob
  3. fset=glob.glob("*.cfg")
  4. for i in fset:
  5.     print i,'handled'

  6. fori=open(i,'r')

  7. line=[]
  8. for line in str(fori.read()).split():
  9.     if "StartTime=" in line:
  10.         line=line.replace(line,'StartTime=1204144200')
  11.         print line,'替换一次'
  12.         fNew = open('1.cfg','a')
  13.         fNew.write(line)
  14.         fNew.write('\n')
  15.         fNew.close()
  16.     else:
  17.         fNew = open('1.cfg','a')
  18.         fNew.write(line)
  19.         fNew.write('\n')
  20.         fNew.close()
复制代码

论坛徽章:
0
8 [报告]
发表于 2009-03-11 13:03 |只看该作者
原帖由 caesarok 于 2009-3-11 11:54 发表
文件太大,行数太多了,

import re
li=[]
f=open("log.txt",'r')
for i in f:
    if re.search("StartTime=[0-9]*",i):
        i=re.sub("StartTime=[0-9]*","StartTime=1204144200",i)
    li.append(i)
f.close()
f=open("log2.txt","w")
f.writelines(li)
f.close()

论坛徽章:
0
9 [报告]
发表于 2009-03-11 13:09 |只看该作者
可能你没明白我的意思

line = f.readline()
if line.startswith('StartTime='):
    line = 'StartTime=1204144200'

干嘛非要replace呢?

论坛徽章:
0
10 [报告]
发表于 2009-03-11 13:43 |只看该作者
原帖由 caesarok 于 2009-3-11 13:02 发表
试出来了

#-*-coding=gbk-*-
import glob
fset=glob.glob("*.cfg")
for i in fset:
    print i,'handled'

fori=open(i,'r')

line=[]
for line in str(fori.read()).split():
    if "StartTime ...

你这个严格来说不行的
>>> s="""1 23456
7"""
>>> print s.split()
['1', '23456', '7']
除非你的log里没空格
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP