python处理PHP数组文本文件实例

所属分类: 脚本专栏 / python 阅读数: 1738
收藏 0 赞 0 分享

需求:

对一个配置文件进行处理,拿出可用的字符来拼接,下面是原始文本,我们要得到这样的结果,

复制代码 代码如下:

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

原始文本:

复制代码 代码如下:

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

更多精彩内容其他人还在看

使用keras实现非线性回归(两种加激活函数的方式)

这篇文章主要介绍了使用keras实现非线性回归(两种加激活函数的方式),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python线性插值解析

这篇文章主要介绍了python线性插值解析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python中flatten( ),matrix.A用法说明

这篇文章主要介绍了Python中flatten( ),matrix.A用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python cv2.resize函数high和width注意事项说明

这篇文章主要介绍了python cv2.resize函数high和width注意事项说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python 图像插值 最近邻、双线性、双三次实例

这篇文章主要介绍了python 图像插值 最近邻、双线性、双三次实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python-openCV开运算实例

这篇文章主要介绍了Python-openCV开运算实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python使用opencv resize图像不进行插值的操作

这篇文章主要介绍了python使用opencv resize图像不进行插值的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Python-OpenCV消除图像中孤立的小区域操作

这篇文章主要介绍了使用Python-OpenCV消除图像中孤立的小区域操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

python右对齐的实例方法

在本篇文章里小编给大家整理的是关于python右对齐的实例方法,有需要的朋友们可以学习参考下。
收藏 0 赞 0 分享

python主要用于哪些方向

在本篇文章里小编给大家整理了一篇关于python用于的方向的相关文章,有需要的阅读下吧。
收藏 0 赞 0 分享
查看更多