Python 如何转换我的';拆分';将代码转换为正则表达式

Python 如何转换我的';拆分';将代码转换为正则表达式,python,regex,list,dictionary,Python,Regex,List,Dictionary,我有一个raw.txt,在下面 ok: [192.168.1.1] => { "OS": "Ubuntu(Core) " } ok: [192.168.1.2] => { "OS": "Ubuntu (Core) " } ok: [192.168.1.3] => { "OS": "CentOS (Core) " } ok: [192.168.1.3] => { "OS":"CentOS (Core) " } ok: [192.168

我有一个
raw.txt
,在下面

ok: [192.168.1.1] => {
    "OS": "Ubuntu(Core) "
}
ok: [192.168.1.2] => {
    "OS": "Ubuntu (Core) "  
}
ok: [192.168.1.3] => {
    "OS": "CentOS (Core) "
}
ok: [192.168.1.3] => {
    "OS":"CentOS (Core) "  
}
ok: [192.168.1.5] => {
    "OS": "Red Hat(Core) "
}
ok: [192.168.1.6] => {
    "OS": "CentOS (Core) "  
}
下面是我的Python代码,介绍如何转换为

f = open(r'raw.txt', 'r')
s = f.read()
list1 = s.split('\n')
ip_list = []
os_list = []
for i in list1[::3]:
    ip_list.append(i)
for i in list1[1::3]:
    os_list.append(i)
y = [z[10:25] for z in os_list]
os_l = [x.strip(' ').replace('"','').replace(' ','') for x in y]
ip_l = [z[5:18] for z in ip_list]
ip_l_rep = [x.strip(' ').replace(']','') for x in ip_l]
{ip_l_rep[n]:os_l[n] for n in range(len(os_l))}
我的输出和期望值如下

{'192.168.1.1': 'Ubuntu(Core)',
 '192.168.1.2': 'Ubuntu(Core)',
 '192.168.1.3': 'CentOS(Core)',
 '192.168.1.5': 'RedHat(Core)',
 '192.168.1.6': 'CentOS(Core)'}
由于这个程序中使用了多个操作,我决定在regex的帮助下编写。我写了一些伪代码,但没有成功。类似于提取
\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}


对我的代码的任何增强也很受欢迎

您可以使用正则表达式捕获介于
[]
之间的内容,然后在
“OS”之后捕获内容“


这会从引号之间的文本中删除不必要的空格。“:


你为什么要用正则表达式呢?当它已经在使用splitwhat is flag=re时。S@cdac--显然与其他解决方案中使用的DOTALL标志相同,因为re.S(或DOTALL)表示“.”也将匹配行尾(因此它将真正匹配任何字符)。因此,“*?”将匹配任何字符(包括行尾)直到下一个匹配。默认情况下,当您执行此操作时,“*?”将匹配任何字符的匹配顺序,但传递此标志时,
匹配
\n
也不包括
import re
input = """
ok: [192.168.1.1] => {
    "OS": "Ubuntu(Core) "
}
ok: [192.168.1.2] => {
    "OS": "Ubuntu (Core) "
}
ok: [192.168.1.3] => {
    "OS": "CentOS (Core) "
}
ok: [192.168.1.3] => {
    "OS":"CentOS (Core) "
}
ok: [192.168.1.5] => {
    "OS": "Red Hat(Core) "
}
ok: [192.168.1.6] => {
    "OS": "CentOS (Core) "
}
"""
items = re.findall(r'\[(.*?)\].*?"OS": "(.*?)"', input, flags=re.S)
data = dict(items)  # only works as you have 2 items (IP, OSTYPE)

print(data)
# output: {'192.168.1.1': 'Ubuntu(Core) ', '192.168.1.2': 'Ubuntu (Core) ', '192.168.1.3': 'Red Hat(Core) ', '192.168.1.6': 'CentOS (Core) '}
import re

f = open(r'raw.txt', 'r')
text = f.read()
f.close()

pattern = r'\[(.+?)\].+?:\s*"\s*(.+?)\s*"'
result = dict(re.findall(pattern, text, flags=re.DOTALL))
print(result)
# {'192.168.1.1': 'Ubuntu(Core)', '192.168.1.2': 'Ubuntu (Core)', '192.168.1.3': 'CentOS (Core)', '192.168.1.5': 'Red Hat(Core)', '192.168.1.6': 'CentOS (Core)'}