1.fill()
作用:取文本作为输入,生成格式化的文本作为输出。
#!/usr/bin/env python import textwrap text = ‘‘‘ China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. ‘‘‘ print textwrap.fill(text,width=30)
执行结果:
China‘s Yaogan-27 remote sensing satellite
was sent into space on Thursday at 10:31 a.m.
Beijing Time, from Taiyuan launch site in
Shanxi Province, north China.
China‘s Yaogan-27 remote
sensing satellite was
sent into space on Thursday at
10:31 a.m. Beijing Time,
from Taiyuan launch site in
Shanxi Province, north China.
文本只有第一行保留了缩进,其余各行前面的空格嵌入到了段落中。
2.dedent()
作用:去除缩进,删除前面的空白符
#!/usr/bin/env python import textwrap text = ‘‘‘ China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. ‘‘‘ dedent_text = textwrap.dedent(text) print text print dedent_text
执行结果:
China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China.
3.结合dedent和fill
#!/usr/bin/env python import textwrap text = ‘‘‘ China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. ‘‘‘ print text dedent_text = textwrap.dedent(text).strip() for width in [30,50]: print ‘%d columns:\n‘ % width print textwrap.fill(dedent_text,width=width) print
执行结果:
China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. 30 columns: China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. 50 columns: China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China.
4.悬挂缩进
可以控制第一行的缩进
#!/usr/bin/env python import textwrap text = ‘‘‘ China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. ‘‘‘ print text dedent_text = textwrap.dedent(text).strip() for width in [30,50]: print ‘%d columns:\n‘ % width print textwrap.fill(dedent_text, initial_indent=‘‘, subsequent_indent=‘ ‘ * 4, width=width,) print
执行结果:
China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. 30 columns: China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. 50 columns: China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China.
悬挂缩进符前面可以加前缀*等符号
#!/usr/bin/env python import textwrap text = ‘‘‘ China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. ‘‘‘ print text dedent_text = textwrap.dedent(text).strip() for width in [30,50]: print ‘%d columns:\n‘ % width print textwrap.fill(dedent_text, initial_indent=‘*‘, subsequent_indent=‘*‘ * 4, width=width,) print
执行结果:
China‘s Yaogan-27 remote sensing satellite was sent into space on Thursday at 10:31 a.m. Beijing Time, from Taiyuan launch site in Shanxi Province, north China. 30 columns: *China‘s Yaogan-27 remote ****sensing satellite was ****sent into space on ****Thursday at 10:31 a.m. ****Beijing Time, from Taiyuan ****launch site in Shanxi ****Province, north China. 50 columns: *China‘s Yaogan-27 remote sensing satellite was ****sent into space on Thursday at 10:31 a.m. ****Beijing Time, from Taiyuan launch site in ****Shanxi Province, north China.
备注:indent():作用缩进,与dedent()正好相反
时间: 2024-12-13 14:41:53