python处理文本例子1
2014-05-19 10:38
274 查看
需求: 对一个配置文件进行处理,拿出可用的字符来拼接,下面是原始文本,我们要得到这样的结果, 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"]
'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"]
相关文章推荐
- python小例子之3 -- 解析xml文本
- Python 文件(file)处理 例子
- awk, python, perl文本处理效率对比(zz)
- 一个文本处理效率小例子
- NLP with python 3 处理原始文本
- Python 中的文本处理
- 【语言处理与Python】2.1获取文本语料库
- 【Python学习与实践之一】文本处理293
- python 文本处理2
- 【语言处理与Python】1.1文本和单词
- Python学习笔记---模块;文件与文本处理
- 可爱的 Python:Python 中的文本处理
- python文本处理
- 工作中处理文本的python代码片段
- python中的文本处理1 草稿
- Python入门的36个例子 之 33 -> try – 异常是不可避免的,关键是怎样处理
- windows下 python 关于管通道(程序回显)处理的一个小例子
- Perl与Python处理文本数值的一个比较
- 流行的脚本语言Python 中的文本处理
- 【语言处理与Python】1.2将文本当作词链表