简体   繁体   English

从多行字符串python获取所需的值

[英]Get required values from multiline string python

I have the following string in python: 我在python中有以下字符串:

Data sent by: zabbix

The production server prod1 is unavailable

https://zabbix.com/link/to/zabbix/dashboard

Environment: prod1
Dep: dev

How can i get Environment and Dep from that string to generate url from them? 我如何从该字符串获取Environment和Dep以从它们生成URL? I tried to go line by line but it seems not a good solution. 我试图逐行走,但这似乎不是一个好的解决方案。 Please help i'm novice in python. 请帮助我是python新手。 Thanks 谢谢

I mean, going through line by line isn't wrong. 我的意思是,逐行检查是没有错的。 Another option is to use regex 另一种选择是使用正则表达式

import re
text = '''Data sent by: zabbix

The production server prod1 is unavailable

https://zabbix.com/link/to/zabbix/dashboard

Environment: prod1
Dep: dev'''

env = re.search(r'Environment: (.+)', text).group(1)
dep = re.search(r'Dep: (.+)', text).group(1)
url = 'zabbix.com/link/to/zabbix/dashboard/{}/{}'.format(dep, env)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM