这是命令vnstat之前pexpect。的输出。我将命令(vnstat)的输出存储在str1中,如下所示。示例:
p.sendline('vnstat')
str1 = p.before
现在,str1包含以下输入数据。
rx / tx / total / estimated
br-int: Not enough data available yet.
eno1:
Oct '19 17.30 TiB / 18.22 TiB / 35.52 TiB / 78.89 TiB
yesterday 1.61 TiB / 1.70 TiB / 3.31 TiB
today 1.99 TiB / 2.05 TiB / 4.04 TiB / 5.50 TiB
eno3: Not enough data available yet.
eno2: Not enough data available yet.
eno4:
Oct '19 155.02 MiB / 5.16 MiB / 160.18 MiB / 355.00 MiB
yesterday 19.46 MiB / 1.08 MiB / 20.54 MiB
today 14.21 MiB / 650 KiB / 14.84 MiB / --"
我正在尝试将上述数据转换为如下所示的字典格式。我想使用冒号(:)转换上面的输入数据。
Dict1_Key{0} = br-int
Dict1_Value{0} = Not enough data available yet.
Dict1_Key{1} = eno1
Dict1_Value{2} = Oct '19 17.30 TiB 18.22 TiB 35.52 TiB 78.89 TiB
yesterday 1.61 TiB 1.70 TiB 3.31 TiB
today 1.99 TiB 2.05 TiB 4.04 TiB 5.50 TiB
...
..
我不确定您的问题是什么,但是在这种字符串中,主要问题是将字符串拆分为以:
开头的几行行]
[当字符串将被分割时,然后在每个组中将易于使用:
创建key
和value
此示例代码将文本分成行和组行
text = ''' rx / tx / total / estimated br-int: Not enough data available yet. eno1: Oct '19 17.30 TiB / 18.22 TiB / 35.52 TiB / 78.89 TiB yesterday 1.61 TiB / 1.70 TiB / 3.31 TiB today 1.99 TiB / 2.05 TiB / 4.04 TiB / 5.50 TiB eno3: Not enough data available yet. eno2: Not enough data available yet. eno4: Oct '19 155.02 MiB / 5.16 MiB / 160.18 MiB / 355.00 MiB yesterday 19.46 MiB / 1.08 MiB / 20.54 MiB today 14.21 MiB / 650 KiB / 14.84 MiB / --''' all_groups = [] # list for all groups group = [] # create list for first group for line in text.split('\n'): if ':' in line: # line with `:` starts new group all_groups.append(group) # add previous group to list of all groups group = [] # create empty list for new group group.append(line.strip()) # add line to current group all_groups.append(group) # add last group to list of groups # --- display groups --- for group in all_groups: text = '\n'.join(group) print(text) print('---')
结果
rx / tx / total / estimated --- br-int: Not enough data available yet. --- eno1: Oct '19 17.30 TiB / 18.22 TiB / 35.52 TiB / 78.89 TiB yesterday 1.61 TiB / 1.70 TiB / 3.31 TiB today 1.99 TiB / 2.05 TiB / 4.04 TiB / 5.50 TiB --- eno3: Not enough data available yet. --- eno2: Not enough data available yet. --- eno4: Oct '19 155.02 MiB / 5.16 MiB / 160.18 MiB / 355.00 MiB yesterday 19.46 MiB / 1.08 MiB / 20.54 MiB today 14.21 MiB / 650 KiB / 14.84 MiB / -- ---
现在很容易将evey组(具有
:
)分为key
和value
。
由于在词典中使用数字键作为键,因此可以使用列表代替字典。并且您可以将key
和value
保留为元组。
data = [] for group in all_groups: text = '\n'.join(group) if ':' in text: key, value = text.split(':') value = value.strip() data.append((key, value)) #print('---', key, '---') #print(value) for item in data: print(item)
结果
('br-int', 'Not enough data available yet.') ('eno1', "Oct '19 17.30 TiB / 18.22 TiB / 35.52 TiB / 78.89 TiB\nyesterday 1.61 TiB / 1.70 TiB / 3.31 TiB\ntoday 1.99 TiB / 2.05 TiB / 4.04 TiB / 5.50 TiB") ('eno3', 'Not enough data available yet.') ('eno2', 'Not enough data available yet.')
最终,您可以使用
br-int
,eno1
等作为字典中的键而不是数字
最终,您可以使用正则表达式来分割文本-data = {} for group in all_groups: text = '\n'.join(group) if ':' in text: key, value = text.split(':') value = value.strip() data[key] = value #print('---', key, '---') #print(value) for item in data.items(): print(item)
EDIT:
re.split('\n(.*):', text)
text = ''' rx / tx / total / estimated
br-int: Not enough data available yet.
eno1:
Oct '19 17.30 TiB / 18.22 TiB / 35.52 TiB / 78.89 TiB
yesterday 1.61 TiB / 1.70 TiB / 3.31 TiB
today 1.99 TiB / 2.05 TiB / 4.04 TiB / 5.50 TiB
eno3: Not enough data available yet.
eno2: Not enough data available yet.
eno4:
Oct '19 155.02 MiB / 5.16 MiB / 160.18 MiB / 355.00 MiB
yesterday 19.46 MiB / 1.08 MiB / 20.54 MiB
today 14.21 MiB / 650 KiB / 14.84 MiB / --'''
import re
results = re.split('\n(.*:)', text)
results = results[1:] # skip first element (first line)
#for item in results:
# print('>', item)
data = []
for key, value in zip(results[0::2], results[1::2]):
data.append((key, value))
#print('---', key, '---')
#print(value)
for item in data:
print(item)