配置文件中的Python搜索和替换

配置文件中的Python搜索和替换,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我想搜索关键字application.baseUrl、baseUrl、主机名和端口,并替换它的现有值 我的代码只能用于前两行application.baseUrl,baseUrl,但不能用于jason格式的参数。如何找到jason格式的主机名和端口来替换它们的值 下面是我的代码 file name : abc.config application.baseUrl="http://ip:9000" baseUrl="http://ip:9000" remote { log-rec

我想搜索关键字application.baseUrl、baseUrl、主机名和端口,并替换它的现有值

我的代码只能用于前两行application.baseUrl,baseUrl,但不能用于jason格式的参数。如何找到jason格式的主机名和端口来替换它们的值

下面是我的代码

file name : abc.config

application.baseUrl="http://ip:9000"

baseUrl="http://ip:9000"

 remote {

    log-received-messages = on

    netty.tcp {
      hostname = "ip"
      port = 9999
      send-buffer-size = 512000b
      receive-buffer-size = 512000b
      maximum-frame-size = 512000b
      server-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
      client-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
    }
  }
这对我有用。我将
=
添加到
应用程序.baseUrl
部分,并添加了一种维护空白的简单方法,以便生成的配置文件保持相同的缩进

newConf
中的值如下所示:

ip="192.168.0.100"
reps= {'application.baseUrl=': """application.baseUrl="http://"""+ip+""":9000""",
       'baseUrl=': """baseUrl="http://"""+ip+""":9000""",
       'hostname': """hostname = \""""+ip+'"',    
       'port': 'port = 8888'}

f = open('/opt/presentation/conf/application.conf','r').read()
lines = f.split("\n")    
newConf = ""    
for line in lines:
    REPLACED = False
    print "checking line: [%s]"%line
    for key in reps.keys():    
        if key in line:
            print "replacing [%s] with [%s]"%(line, reps[key])
            #maintaing white spacing
            count = line.index(key[0])

            l = "%s%s\n"%(" "*count,reps[key])
            REPLACED = True

    if REPLACED == True:
        newConf += l
    else:
        newConf += "%s\n"%line

new_conf_file = open('/opt/presentation/conf/application.conf','w')
new_conf_file.write(newConf)
new_conf_file.close()
你也可以随时为自己做一些工作。下面是一个使用Python的
re
模块的快速示例:

application.baseUrl="http://192.168.0.100:9000

baseUrl="http://192.168.0.100:9000

 remote {

    log-received-messages = on

    netty.tcp {
      hostname = "192.168.0.100"
      port = 8888
      send-buffer-size = 512000b
      receive-buffer-size = 512000b
      maximum-frame-size = 512000b
      server-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
      client-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
    }
  }

不相关,但只需将
用于密钥输入代表
,您也可以在
f
上迭代,我想将更改写回同一文件。不是打印。我只是用一个解决方案更新了我的答案,该解决方案将读取
application.conf
文件,进行更改,然后将更改保存到同一个文件
application.conf
,我在发布后实际解决了这个问题。谢谢你的更新,Max!谢谢你的帮助。嗨,麦克斯。另一个帮助。如何仅替换值。找到键并只替换它的值,而不是整行。可以使用正则表达式只匹配字符串的某些部分,然后替换它。我用一个非常简单的例子更新了我的答案。
application.baseUrl="http://192.168.0.100:9000

baseUrl="http://192.168.0.100:9000

 remote {

    log-received-messages = on

    netty.tcp {
      hostname = "192.168.0.100"
      port = 8888
      send-buffer-size = 512000b
      receive-buffer-size = 512000b
      maximum-frame-size = 512000b
      server-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
      client-socket-worker-pool {
        pool-size-factor = 4.0
        pool-size-max = 64
      }
    }
  }
import re

#this is just a small example of your configuration file string
data = """application.baseUrl='http://192.168.1.100:9000'"""

##compile a re pattern that creates a "group" of the data we we want to replace
##that is comprised of only the numbers 0-9, a period and a colon.
pattern = re.compile("application.baseUrl='http://([0-9\.\:]*)'")
##see if we have a match
match = re.match(pattern, data)
if match:
   ##if we have a match then grab the ip and port number from the re "group"
   ip_and_port = match.group(1)
   print ip_and_port
   ##now replace the old data with the new data
   new_data = data.replace(ip_and_port, "111.222.333.444:65535")
   print new_data