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