转换与弹性制表符缩进/对齐的文本
ElasticTabstops的Python项目详细描述
说明
这个包提供了处理与弹性制表符缩进/对齐的文本的功能。如果有一个编辑器使用弹性制表位,但在使用空格的项目上工作,则可以使用该编辑器将文件从使用空格转换为使用弹性制表位,在启用弹性制表位的编辑器中编辑该文件,然后在保存到磁盘之前将其转换回使用空格。
或者,它可以是从数据表创建文本的一种好方法。
用法
用要转换的字符串实例化一个文本对象,然后调用from_方法之一创建一个表对象。表对象必须使用可以调用的方法来创建新的文本对象。
文本方法是from_space、from_elastic_tabstops和from_fixed_tabstops,而表方法是to_space、to_elastic_tabstops和to_fixed_tabstops。
因此,要将文本从使用空格转换为使用带有弹性制表符的制表符,可以使用以下命令:
fromelastictabstopsimportTextelastic_text=Text(spaces_text).from_spaces().to_elastic_tabstops()
然而,要将文本从使用带有弹性制表位的制表符转换为使用空格,可以使用以下方法:
fromelastictabstopsimportTextspaces_text=Text(elastic_text).from_elastic_tabstops().to_spaces()# or alternativelyspaces_text=Text(elastic_text).from_elastic_tabstops().to_spaces(multiples_of_tab_width=True)
如果要使用此软件包打印字符串表,可以使用以下方法:
fromelastictabstopsimportTablemy_table=[['Title','Author','Publisher','Year'],['Generation X','Douglas Coupland','Abacus','1995'],['Informagic','Jean-Pierre Petit','John Murray Ltd','1982'],['The Cyberiad','Stanislaw Lem','Harcourt Publishers Ltd','1985'],['The Selfish Gene','Richard Dawkins','Oxford University Press','2006'],]spaces_text=Table(my_table).to_spaces()# or if you're displaying the text in a widget which understands elastic tabstopselastic_text=Table(my_table).to_elastic_tabstops()
如果您想要从中获取表格的对齐文本,可以执行以下操作:
fromelastictabstopsimportTexttable=Text(elastic_text).from_elastic_tabstops()table=Text(fixed_text).from_fixed_tabstops()table=Text(spaces_text).from_spaces()