在Python中将字符串解析为不同格式
我有一个文本文档,需要在数组中的关键字之前添加两个@符号 示例文本和数组:在Python中将字符串解析为不同格式,python,python-3.x,Python,Python 3.x,我有一个文本文档,需要在数组中的关键字之前添加两个@符号 示例文本和数组: str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of t
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need, @@name: George @@employee_id:14296 @@blood_group:b positive this is the blood group of the employee @@age:32"
所需文本:
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need, @@name: George @@employee_id:14296 @@blood_group:b positive this is the blood group of the employee @@age:32"
只需使用
replace
功能即可
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr = ['name','employee_id','blood_group','age']
for w in arr:
str = str.replace(w, f'@@{w}')
print(str)
您只需在arr上循环并使用str.replace函数:
for repl in arr:
strng.replace(repl, '@@'+repl)
print(strng)
但是,我建议您更改变量名
str
,因为它是一个保留关键字。您可以替换该值,并在替换的值之前添加空格和double@@并在结果中将双空格替换为一个空格
str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']
for i in arr:
str = str.replace(i, " @@{}".format(i))
print(str.replace(" ", " "))
输出
This is a sample text document which consists of all demographic information of employee here is the value you may need, @@name: George @@employee_id:14296 @@blood_group:b positive this is the blood group of the employee @@age:32
您可以按照以下方式使用
re
模块执行该任务
import re
txt = "This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']
newtxt = re.sub('('+'|'.join(arr)+')',r'@@\1',txt)
print(newtxt)
输出:
This is a sample text document which consists of all demographic information of employee here is the value you may need,@@name: George @@employee_id:14296@@blood_group:b positive this is the blood group of the employee@@age:32
说明:在这里,我使用正则表达式从列表中捕获单词,并将每个单词替换为@@word。这是单次传递,而不是使用多个str.replace时的X次传递(其中X是
arr
),因此在arr
较长的情况下应该更有效。作为替代方法,您可以在循环中转换以下内容以获得更长的列表。在@之前似乎也有空间
str=str[:str.find(arr[0])]+'@'+str[str.find(arr[0]):]
str=str[:str.find(arr[1])]+'@'+str[str.find(arr[1]):]
str=str[:str.find(arr[2])]+'@'+str[str.find(arr[2]):]
str=str[:str.find(arr[3])]+'@'+str[str.find(arr[3]):]