[英]read a multiline string in python
我需要解析一个字符串并从中读取特定的子字符串。 我需要解析的字符串如下:
domain
(
(device
(console
(xxxxxx)
(XXXXXX)
)
)
)
domain
(
(device
(vfb
(xxxxxx)
(location : 5903)
)
)
)
这只是一个示例字符串。 实际的字符串可能包含许多这样的子字符串。 我需要仅从“ vfb”子字符串获取位置字段的值。 我尝试了findall和搜索功能,如下所示
import re
text=re.search('(device(vfb(.*?)))',stringname)
和
import re
text=re.findall('(device(vfb(.*?)))',stringname,re.DOTALL)
但是我总是得到空字符串。 有一个简单的方法吗? 谢谢
您为什么不只寻找location
键值对?
>>> re.findall(r'(\w+) : (\w+)', s)
[('location', '5903')]
简单的python脚本:
fp = open("input.txt", "r")
data = fp.readlines()
for line in data:
if "location" in line:
print line.split(":")[1].split(")")[0].strip()
fp.close()
re.findall(r'device.*?\(vfb.*\(.*\).*(\(.*?\))', s, re.DOTALL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.