Python str.format字符串以固定的间距开始(Python str.format string start at a fixed spacing)

编程入门 行业动态 更新时间:2024-10-21 09:08:52
Python str.format字符串以固定的间距开始(Python str.format string start at a fixed spacing)

我想输出以下格式:

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

我使用str.format来实现它:

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

我基本上按类型构造字符串。 以上是当第二个字符串超过18个字符空格时,后面的字符串被按下的问题。 有办法解决吗?

或者有没有更好的方法来格式化字符串以前面固定的间距开始?

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?

最满意答案

要执行您想要的操作,需要在三个字段中拆分字符串。 下面使用的语法要求使用Python 3.5或更高版本的PEP 448 - 使用的附加解包概括功能。 该函数将字符串分成正在跨越的三个字段的正确字段宽度:

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)

输出:

addr bit value name type 0123 LONG STRING THAT EXCEEDS 18 SPACES!!!!!!!!!!!!!!!! reg 00 0 0xAD NAME bit

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

更多推荐

本文发布于:2023-07-04 11:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间距   字符串   str   format   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!