如何在python中搜索字符串并从数组中插入多个字符串
有一个场景,我需要搜索存储在字符串数组中的字符串,并从数组中插入多个字符串如何在python中搜索字符串并从数组中插入多个字符串,python,python-2.7,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']] l
# 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”)那样执行数组列表的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
编辑/更新:数组值
//===============================================================
//文件名:
//描述:
//姓名:
//创建的文件:
//===============================================================
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//用户名:
//用户说明:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
你好
//++++++++++++++++++++++++++++++++++++++++++
//用户详细信息实例化
//++++++++++++++++++++++++++++++++++++++++++
endhello:
编辑/更新:编辑后更新的数组值。
//===============================================================
//档案名称:布鲁诺
//描述:
//姓名:
//创建的文件:
//===============================================================
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//用户名:布鲁诺
//用户说明:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hello bruno(输入年龄、输入身高);
//++++++++++++++++++++++++++++++++++++++++++
//用户详细信息实例化
//++++++++++++++++++++++++++++++++++++++++++
喂,布鲁诺
按照我的要求让它工作。只是张贴供他人参考
#!/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
#/usr/intel/bin/python2.7
#全局导入变量
导入Tkinter、Tkconstants、tkFileDialog
从Tkinter进口*
全球tmp
tmp=无
列表1=[]
列表_insert=['input','age',['input','height',['input','size']]
列表1.追加('/==================================================================================================================================================================================================================================================================================================================================================================
list1.append('//文件名:bruno')
list1.append('//Desctiption:')
list1.append(“//名称:”)
list1.append(“//创建的文件:”)
列表1.追加('/==================================================================================================================================================================================================================================================================================================================================================================
列表1.追加(“”)
列表1.追加('/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++)
list1.append(“//用户名:bruno”)
list1.append(“//用户描述:”)
列表1.追加('/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++)
列表1.追加(“”)
list1.append('hello bruno;')
列表1.追加('/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
list1.append(“//用户详细信息实例化”)
列表1.追加('/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
list1.append('endhello:bruno')
列表_final=[]
#想在hello name后面插入输入年龄、输入高度的括号
#例如输出:hello name(输入年龄、输入高度);
对于列表1中的行:
if lines.startswith(“hello”):
#打印('hello name({},{})'.format(''.join(list_insert[0]),'.join(list_insert[1]))
#如果(重新搜索(r'hello',行)):
tmp=lines.split()
numelem=len(列表插入)
对于范围内的i(numelem):
如果i==0:
tmpstr=(列表插入[i][0]+''+列表插入[i][1]+'))
elif i==numelem-1:
tmpstr=(“(“+list_insert[i][0]+”+list_insert[i][1]+”,”)
其他:
tmpstr=(列表插入[i][0]+''+列表插入[i][1]+',')
tmp.插入件(2,tmpstr)
列表_final.append(“”.join(tmp))
其他:
列表\最终追加(行)
hello_file_ptr=打开(“hello_file”,“w”)
对于列表_final中的行:
#hello_file_ptr.write(行)
打印行
输出:
//===============================================================
// 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
//===============================================================
//档案名称:布鲁诺
//描述:
//姓名:
//创建的文件:
//===============================================================
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//用户名:布鲁诺
//用户说明:
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hello bruno(输入大小、输入高度、输入年龄);
//++++++++++++++++++++++++++++++++++++++++++
//用户详细信息实例化
//++++++++++++++++++++++++++++++++++++++++++
喂,布鲁诺
很难理解,你想要什么。数组中的字符串是什么,你想从哪个数组中插入多个字符串。也许你的意思是“为列表中的项目插入:做点什么”?list_insert包含要插入的字符串。我想在特定行中找到hello name后插入它们。如果您看到例如:hello name(输入年龄,输入高度);需要插入。这有帮助吗?打印('hello name({},{}')。格式(''.join(list_insert[0]),'.join(list_insert[1]))打印名称很好,但列表1将包含更多的行作为字符串。我需要搜索以hello.*开头的字符串,并替换为hello(插入数组内容);对我来说,您似乎试图以复杂的方式解决一个简单的问题。因此,我希望您能更详细地解释您的要求。