Python processes PHP array text files,

Source: Internet
Author: User

Python processes PHP array text files,

Requirements:

Process a configuration file and splice the available characters. The following is the original text. We want to get this result,
Copy codeThe Code is 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 codeThe Code is as follows:
'Redis _ list' => array (
'Normal' => array (
'Host' => '10. 4.3.125 ',
'Port' => 6405,
'Db' => 6
),
'Redis _ list' => array (
'Normal' => array (
'Host' => '2017. 0.0.1 ',
'Port' => 6379,
'Db' => 2
),
'Redis _ list' => array (
'Normal' => array (
'Host' => '2017. 0.0.1 ',
'Port' => 6379,
'Db' => 16
),
'Redis _ list' => array (
'Normal' => array (
'Host' => '2017. 0.0.1 ',
'Port' => 6379,
'Db' => 8
),

Python:

Copy codeThe Code is 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' => 127, '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.