python處理PHP數組文本文件實例
需求:
對一個配置文件進行處理,拿出可用的字符來拼接,下面是原始文本,我們要得到這樣的結果,
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"]
相關文章
用Python Flask創(chuàng)建簡潔高效的URL短鏈接服務
本文介紹了如何使用Python Flask框架創(chuàng)建URL短鏈接服務。通過詳細的步驟和代碼示例,讀者將學會如何搭建一個高效的URL縮短服務,包括生成短鏈接、重定向、還原長鏈接等功能。本文還介紹了如何使用Redis數據庫實現短鏈接的存儲和管理和如何優(yōu)化短鏈接的訪問速度和可靠性2023-04-04Pandas 重塑(stack)和軸向旋轉(pivot)的實現
這篇文章主要介紹了Pandas 重塑(stack)和軸向旋轉(pivot)的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07