python处理PHP数组文本文件实例
需求:
对一个配置文件进行处理,拿出可用的字符来拼接,下面是原始文本,我们要得到这样的结果,
redis-h127.0.0.1-p6379|select2 redis-h127.0.0.1-p6379|select16 redis-h127.0.0.1-p6379|select8
原始文本:
'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/envpython #coding=utf-8 importos
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("),","") #printpstr pstr_list=pstr.split("redis_list=>array") #printtype(pstr_list)
cf_param=[] foriinpstr_list: ifi: ##'host'=>'127.0.0.1','port'=>6411,'db'=>2 i_list=i.split(",") iflen(i_list)==3: op={}; foriiini_list: ii_list=ii.split("=>") iflen(ii_list)==2: op[ii_list[0]]=ii_list[1] cf_param.append(op)
foriincf_param: print"redis-h"+i["host"]+"-p"+i["port"]+"|select"+""+i["db"]