圆月山庄资源网 Design By www.vgjia.com

需求:

对一个配置文件进行处理,拿出可用的字符来拼接,下面是原始文本,我们要得到这样的结果,
复制代码 代码如下:
redis -h 127.0.0.1 -p 6379 | select 2
redis -h 127.0.0.1 -p 6379 | select 16
redis -h 127.0.0.1 -p 6379 | select 8

原始文本:

复制代码 代码如下:
    'redis_list' => array(
        'normal' => array(
            'host' => '10.4.3.125',
            'port' => 6405,
            'db' => 6
        ), 
    'redis_list' => array(
        'normal' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'db' => 2
        ), 
    'redis_list' => array(
        'normal' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'db' => 16
        ), 
    'redis_list' => array(
        'normal' => array(
            'host' => '127.0.0.1',
            'port' => 6379,
            'db' => 8
        ),

上python:

复制代码 代码如下:
#!/usr/bin/env python
#coding=utf-8
import os

file=open("redis_list.txt", "r")
file_content=file.read()
php_array=file_content.replace("'normal' => array(","")
pstr = php_array.replace(" ","").replace("\r","").replace("\n", "").replace("\t", "").replace("(", "").replace("'", "").replace("),", "")
#print pstr
pstr_list = pstr.split("redis_list=>array")
#print type(pstr_list)

cf_param = []
for i in pstr_list:
 if i:
  ## 'host'=>'127.0.0.1','port'=>6411,'db'=>2
  i_list = i.split(",")
  if len(i_list)==3:
   op = {};
   for ii in i_list:
    ii_list = ii.split("=>")
    if len(ii_list) == 2:
     op[ii_list[0]] = ii_list[1]
   cf_param.append(op)

for i in cf_param:
    print "redis -h "+i["host"] + " -p "+i["port"] +"|select" +" "+i["db"]

标签:
python,PHP数组,文本文件

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com