2010-06-22 53 views
4

我想格式化一些字符串输出命令行,报告样式,并且正在寻找最简单的方法来格式化一个字符串,以便我可以获得自动段落格式。Python字符串格式化程序段落

在perlform格式通过“格式”功能perlform的

format Something = 
    Test: @<<<<<<<< @||||| @>>>>> 
      $str,  $%, '$' . int($num) 
. 

$str = "widget"; 
$num = $cost/$quantity; 
$~ = 'Something'; 
write; 

变化允许文本被包裹干净完成,帮助屏幕有用,日志报告和这样。

是否有python等价物?或者我可以使用Python的新字符串format函数写一个合理的黑客攻击?

输出示例,我想:

Foobar-Title Blob 
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. 
+0

它是做什么的?对于那些不了解Perl的人。 – kennytm 2010-06-22 19:23:15

+0

@KennyTM - 特别是这个例子?没有太多只是输出一个数字表。我想处理包装一些字符串段。如果你手动执行这些操作,那么行缩进会变成真正的痛苦,所以我希望能够找到可以使用的东西。除非一些Python vodo可以解决这个问题,否则我期望... – Petriborg 2010-06-22 19:25:56

回答

5

Python中没有像这样的自动格式化。 (.format函数语法是从C#中借用的。)毕竟,Perl是“实用提取和报告语言”,而Python并不是为格式化报告而设计的。

您的输出可以使用textwrap模块完成,例如,

from textwrap import fill 
def formatItem(left, right): 
    wrapped = fill(right, width=41, subsequent_indent=' '*15) 
    return ' {0:<13}{1}'.format(left, wrapped) 

... 

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

请注意,这里假设“左”不跨越1行。更一般的解决方案是

from textwrap import wrap 
from itertools import zip_longest 
def twoColumn(left, right, leftWidth=13, rightWidth=41, indent=2, separation=2): 
    lefts = wrap(left, width=leftWidth) 
    rights = wrap(right, width=rightWidth) 
    results = [] 
    for l, r in zip_longest(lefts, rights, fillvalue=''): 
     results.append('{0:{1}}{2:{5}}{0:{3}}{4}'.format('', indent, l, separation, r, leftWidth)) 
    return "\n".join(results) 

>>> print(twoColumn("I'm trying to format some strings for output on the command-line", "report style, and am looking for the easiest method to format a string such that I can get automatic paragraph formatting.")) 
    I'm trying to report style, and am looking for the 
    format some easiest method to format a string such 
    strings for that I can get automatic paragraph 
    output on the formatting. 
    command-line 
+0

非常好,非常明确的例子谢谢。 – Petriborg 2010-06-22 23:12:39

1

有蟒蛇文件thisthis可能会有所帮助。

+0

这个格式化库可能会有效果!根本不知道,现在我正在尝试。 – Petriborg 2010-06-22 19:41:57

+0

@Petriborg - 好运:D – Kyra 2010-06-22 19:44:44

+0

这是用于textwrap的python文档 - http://docs.python.org/library/textwrap.html – Petriborg 2010-06-23 10:40:07

4
import textwrap 
import itertools 

def formatter(format_str,widths,*columns): 
    ''' 
    format_str describes the format of the report. 
    {row[i]} is replaced by data from the ith element of columns. 

    widths is expected to be a list of integers. 
    {width[i]} is replaced by the ith element of the list widths. 

    All the power of Python's string format spec is available for you to use 
    in format_str. You can use it to define fill characters, alignment, width, type, etc. 

    formatter takes an arbitrary number of arguments. 
    Every argument after format_str and widths should be a list of strings. 
    Each list contains the data for one column of the report. 

    formatter returns the report as one big string. 
    ''' 
    result=[] 
    for row in zip(*columns): 
     lines=[textwrap.wrap(elt, width=num) for elt,num in zip(row,widths)] 
     for line in itertools.izip_longest(*lines,fillvalue=''): 
      result.append(format_str.format(width=widths,row=line)) 
    return '\n'.join(result) 

例如:

widths=[17,41] 
form='{row[0]:<{width[0]}} {row[1]:<{width[1]}}' 

titles=['Foobar-Title','0123','hi there','something'] 
blobs=['Blob','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.','dito','more text here. more text here. more text here.'] 

print(formatter(form,widths,titles,blobs)) 

产生

# Foobar-Title  Blob          
#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.          

formatter可以采取列的任意数量。

+0

感谢@unutbu这也是一个明确的例子。希望我能接受更多的答案,哈哈。 – Petriborg 2010-06-22 23:16:41