I have some text for example:
我有一些文字例如:
'This is a line of text over 10 characters'
That I need to be broken into lines consisting of no more than 10 characters without breaking words unless I need to (for example a line with work containing more than 10 characters).
除非我需要(例如包含超过10个字符的工作的行),否则我需要将其分成不超过10个字符的行而不会破坏单词。
The line above would turn into:
上面的一行会变成:
'This is a\nline of\ntext over\n10\ncharacters'
It's a fairly simple problem but I'd like to hear how people would do it. I'm going to start coding it and post my solution in a little while as well.
这是一个相当简单的问题,但我想听听人们会怎么做。我将开始编码并在一段时间后发布我的解决方案。
1 个解决方案
#1
16
You need textwrap
你需要textwrap
>>> import textwrap
>>> s = 'This is a line of text over 10 characters'
>>> textwrap.fill(s, width=10)
'This is a\nline of\ntext over\n10\ncharacters'
#1
16
You need textwrap
你需要textwrap
>>> import textwrap
>>> s = 'This is a line of text over 10 characters'
>>> textwrap.fill(s, width=10)
'This is a\nline of\ntext over\n10\ncharacters'