1

I want to output the following format:

addr<-9->bit<-15->value<-13->name<-26->type
....     ...      ......     ......    ......   #..... is the content of each row

I used the str.format to achieve it:

STRING_FORMATTER = '{0:13}{1:18}{2:18}{3:30}{4:10}\n'
content = STRING_FORMATTER.format('addr', 'bit', 'value', 'name', 'type')
content = content + STRING_FORMATTER.format('0123', 'LONG STRING THAT EXCEEDS 18 SPACES!!!!!!!!!!!!!!!!', '', '', 'reg')
content = content + STRING_FORMATTER.format('00', '0', '0xAD', 'NAME', 'bit')
.....

I basically construct the string by the type. The above is a problem that when the 2nd string exceeds 18 char spaces, the string behind is pushed. Is there a way to solve it?

Or is there a better way to format the string to start at a fixed spacing in front?

2
  • Do you want the long string to be cut off? Commented Dec 15, 2017 at 1:29
  • I do not want that to be cut off, I want the Long string to start at 14, and it has a size 18+18+30. And I want the type exactly align with the first line Commented Dec 15, 2017 at 1:33

2 Answers 2

2

To do what you want requires splitting the string across the three fields. The syntax used below requires Python 3.5 or later for the PEP 448 - Additional Unpacking Generalizations feature used. The function breaks a string into the correct field widths for the three fields being spanned:

STRING_FORMATTER = '{0:13}{1:18}{2:18}{3:30}{4:10}\n'

def split(s,*widths):
    current = 0
    for width in widths:
        yield s[current:current + width]
        current += width

content = STRING_FORMATTER.format('addr', 'bit', 'value', 'name', 'type')
content += STRING_FORMATTER.format('0123', *split('LONG STRING THAT EXCEEDS 18 SPACES!!!!!!!!!!!!!!!!',18,18,30), 'reg')
content += STRING_FORMATTER.format('00', '0', '0xAD', 'NAME', 'bit')
print(content)

Output:

addr         bit               value             name                          type
0123         LONG STRING THAT EXCEEDS 18 SPACES!!!!!!!!!!!!!!!!                reg
00           0                 0xAD              NAME                          bit
Sign up to request clarification or add additional context in comments.

Comments

2

You can construct a function to truncate strings that are too long, and wrap that around the format inputs.

def truncate(*inputs, max_length=18):
    return tuple(s[:max_length] for s in inputs)

STRING_FORMATTER = '{0:13}{1:18}{2:18}{3:30}{4:10}\n'
content = STRING_FORMATTER.format(
    *truncate('addr', 'bit', 'value', 'name', 'type'))
content = content + STRING_FORMATTER.format(
    *truncate('0123', 'LONG STRING THAT EXCEEDS 18 SPACES!!!!!!!!!!!!!!!!', '', '', 'reg'))
content = content + STRING_FORMATTER.format(
    *truncate('00', '0', '0xAD', 'NAME', 'bit'))

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.