返回字符串列表*正好*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第三方库


热门话题
java OpenGL着色器可在桌面上编译,但不能在Android上编译   java调度器。forward()生成HTTP 500   java从另一个类访问类   java EasyModBus Modbus客户端。ReadHoldingRegisters()返回意外的0   java 安卓bitmap低质量问题   带有signaturePropFile的java Axis2/rampart加密问题   Java,解析xml属性的原始未转换值。Jaxson或jaxb   爪哇太阳报。网inetaddr。ttl属性导致AccessControlException   java简单计算器无法通过println   线程安全java servlet   java从文本文件中获取特定信息   java打包具有字符串数组的哈希映射   java SQL合并以更新值或将值插入到同一个表中   java springrabitmq和线程本地何时是调用clean方法的好时机   windows java在我的64位计算机的任务管理器中显示为32位   java不确定使用什么值来初始化我的神经网络   java Maven插件下载文件