python - 在 Python 中将字符串解析为不同的格式
问题描述
我有一个文本文档,我需要在数组中的关键字之前添加两个 @ 符号。
示例文本和数组:
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)
推荐阅读
- excel - 仅当范围具有值时在 Excel 中显示标题
- javascript - javascript - if and or (||) 语句
- c# - 从 Xamarin Forms 应用程序写入 Android 上的“文档”文件夹
- powershell-core - 有没有办法在我的脚本中为用户输入显示“控制台选择菜单”,例如 PSReadLine 中的 Ctrl+Space
- netlogo - 按一项排序列表并在Netlogo中识别满足条件的记录
- css - Bootstrap pull-right 在 Safari 的卡头中不起作用,但在 Chrome 中起作用
- node.js - 为什么Mongo会从Secondary读取并将读取关注设置为Primary
- python - 监视文件并在每个新行上迭代 Python 循环
- python-3.x - 无法理解回调和块的python变量范围
- r - 向R中的列表项添加描述