0

This is the output of pexpect.before for the command vnstat. I store the output of the command(vnstat) in str1 as given below. Example:

p.sendline('vnstat')
str1  = p.before

now, the str1 contains the below input data.

                     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  /      --"

I am trying to convert the above data into the dictionary format like the below. Using the colon(:), I want to convert the above input data.

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
...
..
4
  • Hi @karthika, welcome to SO. Please read the rules to create a question that will be answered. Commented Nov 14, 2019 at 16:34
  • where is your code? What result you get with your code? Commented Nov 14, 2019 at 17:56
  • as for me main problem is to split all text in groups of lines which start at line which have :. When lines are grouped then it is easy split group using : Commented Nov 14, 2019 at 18:05
  • In case you are interested of getting the values out from the output, it's worth noting that vnStat also provides a json format output with vnstat --json which is usually far more trivial to parse. Commented Nov 23, 2019 at 23:06

1 Answer 1

1

I'm not sure what is your problem but in this kind of string main problem can be splitting string to groups of lines which start at line with :

When string will be splitted then in every group will be easy to use : to create key and value

This example code splits text in lines and group lines

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('---')

Result

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  /      --
---

Now it is easy to split evey group (which has :) into key and value.

Because you use nubmers as keys in your dictionares then you could use list instead of dictionary. And you could keep key and value together as tuple.

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)

Result

('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.')

Eventually you could use br-int, eno1, etc. as a key in dictionary instead of numbers

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: Eventually you can use regex to split text - 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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.