我下面有一个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)'}
由于此程序中使用了多种操作,因此我决定在正则表达式的帮助下编写。我写了一些伪代码,但没有成功。例如提取\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}
。
我对代码的任何增强也表示赞赏
答案 0 :(得分:4)
您可以使用正则表达式来捕获[]
和"OS": "
之后的内容:
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) '}
答案 1 :(得分:1)
这会在引号"
之间的文本中去除不必要的空格:
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)'}