Demand:
To handle a configuration file, take out the available character stitching, the following is the original text, we want to get this result,
Copy Code code as follows:
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
Original text:
Copy Code code as follows:
' 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
),
On Python:
Copy Code code as follows:
#!/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"]