返回字符串列表*正好*n宽度

JustifyText的Python项目详细描述


证明文本

*对于等距字体
Takes  a  string  and  inserts
spaces  between  words,  until
line is  desired  width,  like
you see here.

导入模块并使用函数:

  • 选项1:
from justifytext import justify
justify(text, width)

  • 选项2:
import justify
justifytext.justify(text, width)

模块要求/依赖性:

  • python code tab=4个空格
  • 需要文本包装模块

模块假设:

  • 行之间至少有一个空格(至少在此版本中)。
  • 依赖于在textwrapper中等于true的长单词break。(您可以更改此模块中的textWrapper函数调用,但在足够小的宽度下,您只会得到后面带有单个单词的行,并且在这些行的末尾只有空格,即不对齐)。

必需的位置参数:

  • text==要格式化为长度为n个字符的字符串列表的字符串(当然,您必须处理引号和特殊字符)。
  • width==结果行的所需宽度

可选参数和默认参数:

justify_last_line = False

这是处理字符串最后一行的默认值。约定是不要在格式化块的范围内拉伸几个单词,但我已经提供了一种方法来撤消它——如果这是您的类型的话。例如:

	text = "The end of this bit of text will be these three words." 

	justify(text, 35) 

将返回一个可循环显示的列表:

	The end of this bit of text will be
	these three words.

鉴于

	justify(text, 35, justify_last_line = True) 

将返回一个iterable列表,该列表将显示:

	The end of this bit of text will be
	these         three          words.

模块流量:

  1. 接受字符串(模块似乎忽略了\n,还不确定原因--我很累。可能与文本包装行为有关。若要保留特殊字符,请对其进行转义,即“n”)。否则应用普通的字符串操作规则,您将需要相应地处理引号和特殊字符。此模块中最早的步骤之一是通过textwrapper输入文本,因此,如果您的输入没有在那里引发textwrapper错误,您应该没事。(可能)。

  2. 使用textwrap和textwrapper分成不超过n行。见1结尾的讨论。上面。

  3. 循环,从最后一个空格开始(实际上,在最后一个空格之前的单词),然后向后走,在单词之间添加一个空格(理论上,当从左到右阅读并在一行的末尾遇到它们时,读者不太可能注意到额外的空格)。

  4. 如果在一行中的所有单词之间添加空格后,一行仍然很短,模块将在最后一个空格处重新开始(low,它输入第二个或第三个或第八个空格-从末尾开始-以达到所需的宽度)。

  5. 一旦所有行的宽度都为n,就返回行列表。模块可以将它们打印到一个漂亮的块中,但是我更愿意将重新格式化的文本交还给您,这样您就可以随心所欲地处理它。要显示重新格式化的文本,一个简单的iterable就可以了。 例如:

	for i in returnText:
	print(i)

示例用法

原始文本:

text = "Now did the Lord say, \"First thou pullest the Holy Pin. Then thou must count to three.
Three shall be the number of the counting and the number of the counting shall be three. Four
shalt thou not count, neither shalt thou count two, excepting that thou then proceedeth to three.
Five is right out. Once the number three, being the number of the counting, be reached, then lobbest
thou the Holy Hand Grenade in the direction of thine foe, who, being naughty in my sight, shall snuff it." 

呼叫:

x = justify(text, 30) 

返回:

x = ['Now did the Lord say, "First', 'thou pullest the Holy Pin.', 'Then thou must count to three.',
'Three shall be the number of', 'the counting and the number of', 'the counting shall be three.', 
'Four shalt thou not count,', 'neither shalt thou count two,', 'excepting that thou then', 'proceedeth
to three.  Five is', 'right out.  Once the number', 'three, being the number of the', 'counting, be
reached, then', 'lobbest thou the Holy Hand', 'Grenade in the direction of', 'thine foe, who, being 
naughty', 'in my sight, shall snuff it.'] 

调整显示的处理列表:

	for i in x: 
	print(i) 

样本输出:

	Now did the Lord  say,  "First
	thou  pullest  the  Holy  Pin.
	Then thou must count to three.
	Three shall be the  number  of
	the counting and the number of
	the counting shall  be  three.
	Four  shalt  thou  not  count,
	neither shalt thou count  two,
	excepting   that   thou   then
	proceedeth to three.  Five  is
	right  out.  Once  the  number
	three, being the number of the
	counting,  be  reached,   then
	lobbest  thou  the  Holy  Hand
	Grenade in  the  direction  of
	thine foe, who, being  naughty
	in my sight, shall snuff it.

其他/杂项:

这个包裹是按照通知准备的,地址是:https://packaging.python.org

找到虫子了吗?告诉我! 邮寄地址:cjtx.code@gmail.com

-CJTX/2019年5月27日

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
HTTP标头的java InputStream未终止   java测试SpringWebListener   具有定义的替换编号Java的regex ReplaceAll   在java中使用contains()方法   java在选择查询中使用JoinColumn字段   具有用户的java Start-stop demon不是以给定用户启动   java glBufferData生成GL\u无效\u操作   java中循环代码的循环   Java位无符号移位(>>>>)会产生奇怪的结果   java HQL使用点分隔符从select获取结果   条纹、弹簧、玩耍(或?):使用哪种高性能Java框架?   广播接收机中的java停止服务   java回收器视图占据整个屏幕。不在上面显示我的UI元素   java使MySQL查询更快   java MappedByteBuffer查询   java递归算法问题