如何在python中从数组中搜索字符串并插入多个字符串

时间:2018-08-01 05:57:25

标签: python python-2.7

有一种情况,我需要搜索存储在字符串数组中的字符串并从数组中插入多个字符串。

# Global Import Variables
import Tkinter, Tkconstants, tkFileDialog
from   Tkinter import *
import subprocess
import re

list1 = [] 
list2 = ['cat','tiger']
list_insert = [['input', 'age'],['input', 'height']]

list1.append(['wolfgang'] + list2)
print list1 

list1.append('hello name;')
list1.append('endhello: hello')

# Wanted to insert input age, input height after hello name inside braces 
# eg output like :  hello name (input age, input height);

for lines in list1:
    re.sub(r'(hello.*), r'') 

a。是否需要像line.startswith(“ hello”)一样对数组list_insert进行for循环以添加变量?还有其他更好的方法可以快速添加吗?喜欢reg exp吗?请提供您的评论。

EDIT/UPDATE: Array Values
//===============================================================
// File Name        : <USER_NAME>  
// Desctiption      :
// Name             : <CREATE_NAME>
// File Created     : <CREATE_DATE>
//===============================================================

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// User Name            : <USER_NAME>
// User Desctiption     : 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

hello <USER_NAME>;
 //++++++++++++++++++++++++++++++++++++++++++
 // User Details Instantiation 
 //++++++++++++++++++++++++++++++++++++++++++
endhello: <USER_NAME>



EDIT/UPDATE: Updated Array Values after edit.
    //===============================================================
    // File Name        : bruno  
    // Desctiption      :
    // Name             : <CREATE_NAME>
    // File Created     : <CREATE_DATE>
    //===============================================================

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // User Name            : bruno
    // User Desctiption     : 
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    hello bruno (input age, input height);
     //++++++++++++++++++++++++++++++++++++++++++
     // User Details Instantiation 
     //++++++++++++++++++++++++++++++++++++++++++
    endhello: bruno

1 个答案:

答案 0 :(得分:0)

该按我的要求工作了。只是发布以供他人参考!

#!/usr/intel/bin/python2.7

# Global Import Variables
import Tkinter, Tkconstants, tkFileDialog
from   Tkinter import *

global tmp

tmp = None
list1 = [] 
list_insert = [['input', 'age'],['input', 'height'], ['input', 'size']]

list1.append('//===============================================================')
list1.append('// File Name        : bruno')  
list1.append('// Desctiption      :')
list1.append('// Name             : <CREATE_NAME>')
list1.append('// File Created     : <CREATE_DATE>')
list1.append('//===============================================================')
list1.append(' ')
list1.append('//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
list1.append('// User Name            : bruno')
list1.append('// User Desctiption     : ')
list1.append('//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
list1.append(' ')
list1.append('hello bruno ;')
list1.append(' //++++++++++++++++++++++++++++++++++++++++++')
list1.append(' // User Details Instantiation ')
list1.append(' //++++++++++++++++++++++++++++++++++++++++++')
list1.append('endhello: bruno')

list_final = []

# Wanted to insert input age, input height after hello name inside braces 
# eg output like :  hello name (input age, input height);

for lines in list1:
    if lines.startswith("hello "):
        #print( 'hello name( {}, {} )'.format(' '.join(list_insert[0]), ' '.join(list_insert[1])) )
        # if (re.search(r'hello.',lines)):
        tmp = lines.split()
        numelem = len(list_insert)
        for i in range (numelem):
            if i == 0:
                tmpstr = (list_insert[i][0]+' '+list_insert[i][1]+')')
            elif i == numelem-1:
                tmpstr = ('('+list_insert[i][0]+' '+list_insert[i][1]+',')
            else:
                tmpstr = (list_insert[i][0]+' '+list_insert[i][1]+',')
            tmp.insert(2,tmpstr)
        list_final.append(' '.join(tmp)) 
    else:
        list_final.append(lines)      

hello_file_ptr = open("hello_file", "w")

for lines in list_final:
    #hello_file_ptr.write(lines)
    print lines

输出:

//===============================================================
// File Name        : bruno
// Desctiption      :
// Name             : <CREATE_NAME>
// File Created     : <CREATE_DATE>
//===============================================================

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// User Name            : bruno
// User Desctiption     : 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

hello bruno (input size, input height, input age) ;
 //++++++++++++++++++++++++++++++++++++++++++
 // User Details Instantiation 
 //++++++++++++++++++++++++++++++++++++++++++
endhello: bruno