我正在尝试在命令行、报表样式上设置一些字符串的输出格式,并正在寻找设置字符串格式的最简单方法,以便能够获得自动段落格式。
在perlform中,格式化是通过“format”函数完成的
format Something =
Test: @<<<<<<<< @||||| @>>>>>
$str, $%, '$' . int($num)
.
$str = "widget";
$num = $cost/$quantity;
$~ = 'Something';
write;
perlform的变体允许文本被干净地包装,对于帮助屏幕、日志报告等非常有用。
有与python等价的吗?或者我可以使用Python的新字符串format函数编写一个合理的hack?
示例输出我希望:
Foobar-Title Blob
0123 This is some long text which would wrap
past the 80 column mark and go onto the
next line number of times blah blah blah.
hi there dito
something more text here. more text here. more text
here.
Python中没有这样的自动格式化。(函数语法是从C中借用的)毕竟,Perl是“实用的提取和报告语言”,Python不是为格式化报告而设计的。
输出可以使用
textwrap
模块完成,例如注意,这假设“左”不跨越1行。更普遍的解决办法是
python文档中的this和this可能有帮助。
例如:
收益率
formatter
可以接受任意数量的列。相关问题 更多 >
编程相关推荐