Python handles php array text file instance _python

Source: Internet
Author: User
Tags redis

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"]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.