[英]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.