0

I have a slackbot that posts snmp messages sent from network devices.

Example:

rtr-canada-1: BGP Peer 1.1.1.1 - Description: Test route - State: Idle

rtr-us-1: BGP Peer 1.1.1.2 - Description: Very Long name Industries - State: Established.

What's the best way of formatting every message so that the "Description:" and "State:" markers always end up at the same position in the string, therefore making everything uniform?

Example:

rtr-canada-1: BGP Peer 1.1.1.1 - Description: Test route -             State: Idle

rtr-us-1:     BGP Peer 1.1.1.2 - Description: Very Long name Industries - State: Established.

I'm thinking of making a function that measures the length of the string before posting it and then fills the message with whitespace until everything is pushed in place but I wonder if there isn't a smarter way of doing this.

Thanks,

Quake
  • 105
  • 1
  • 10

2 Answers2

0

As described in How can I fill out a Python string with spaces? Python has some functions for this. Have a look at:

https://docs.python.org/3/library/stdtypes.html#str.ljust

Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

https://docs.python.org/3/library/stdtypes.html#str.rjust

Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).

Bas van Dijk
  • 9,933
  • 10
  • 55
  • 91
0

Thank you sir,

This is what I've got. It's working well.

def formattage(text):

    text = text.rsplit("$")
    a = '{:{align}{width}}'.format(text[0], align='<', width='35')
    b = '{:{align}{width}}'.format(text[1], align='<', width='29')
    c = '{:{align}{width}}'.format(text[2], align='<', width='83')
    d = '{:{align}{width}}'.format(text[3], align='<', width='33')
    text = a+b+c+d
    print text
Quake
  • 105
  • 1
  • 10