Python字符串格式化程序的段落 [英] Python string formatter for paragraphs

查看:176
本文介绍了Python字符串格式化程序的段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图格式化一些字符串输出命令行,报告样式,并寻找最简单的方法来格式化字符串,以便我可以自动的段落格式。



在perlform格式化是通过格式化功能完成的
$ b $ $ p $格式Something =
测试:<<<<<<<< @ ||||| @>>>>>
$ str,$%,'$'。 int($ num)


$ str =widget;
$ num = $ cost / $ quantity;
$〜='Something';
写;

perlform的变体允许文本被干净地包装起来,对帮助屏幕,日志报告等很有用。

有没有python的等价物?或者我可以用Python的新字符串格式函数编写一个合理的黑客攻击



示例输出我想要:

  Foobar标题Blob 
0123这是一些很长的文本,它将在80列标记之后包装
并转到
下一行次数等等等等等等。
您好,有dito
更多的文字在这里。这里更多的文字。更多文字
在这里。


解决方案

。 ( .format 函数语法是从C#中借用的)。毕竟,Perl是实用的提取和报告语言,而Python并不是为格式化报告而设计的。 >

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

从textwrap导入填充
def formatItem(左,右):
wrapped = fill(right,width = 41,subsequent_indent =''* 15)
返回'{0:< 13} {1}'。格式(左边,包裹)

...

>>> print(formatItem('0123','这是一些长的文本,将包装超过80列标记,并进入下一行次数等等等等)。)
0123这是一个长文本,这将是把
换成80列标志
,然后转到下一行
次数等于等于
等等。

请注意,这里假定left不能超过1行。一个更通用的解决方案是从textwrap导入包装
从itertools导入zip_longest
def twoColumn(左边)

  ,right,leftWidth = 13,rightWidth = 41,indent = 2,separation = 2):
lefts = wrap(left,width = leftWidth)
rights = wrap(right,width = rightWidth) $ b results = []
for zip_longest(左,右,fillvalue =''):
results.append('{0:{1}} {2:{5}} ('',indent,l,separation,r,leftWidth))
返回\\\
.join(结果)

>>> print(twoColumn(我试图格式化一些字符串输出在命令行上,报告样式,并寻找最简单的方法格式化字符串,以便我可以自动段落格式化。 b $ b我试图报告样式,并且正在寻找
格式的一些最简单的方法来格式化一个字符串,例如
字符串,因为我可以在格式上自动获得段落
的输出。
命令行


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.

In perlform formatting is done through the "format" function

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

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

Variations of the perlform allow texts to be wrapped cleanly, useful for help screens, log reports and such.

Is there a python equivalent? Or a reasonable hack that I could write using Python's new string format function?

Example output I'd like:

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.

解决方案

There isn't automatic formatting like this built into Python. (The .format function syntax is borrowed from C#.) After all, Perl was "Practical Extraction and Report Language" and Python isn't designed for formatting reports.

Your output could be done with the textwrap module, e.g.

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

Note that this assumes the "left" does not span over 1 line. A more general solution would be

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   

这篇关于Python字符串格式化程序的段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆