Python 中的大块字符串,不打断单词

chunk string in Python without breaking words

我有这段代码,用于在 20x2 LCD 显示器上显示一些文本:

#!/usr/bin/python

LCDCHARS = 20
LCDLINES = 2

def WriteLCD(text_per_LCD):
    chunked = (text_per_LCD[i:LCDCHARS+i] for i in range (0, len(text_per_LCD), LCDCHARS))
    count_l = 0
    for text_per_line in chunked:
        # print will be replaced by actual LCD call
        print (text_per_line)
        count_l += 1
        if count_l >= LCDLINES:
            # agree to lose any extra lines
            break

WriteLCD("This text will display on %s LCD lines" % (LCDLINES))

示例字符串将输出

This text will displ
ay on 2 LCD lines

如何拆分字符串而不破坏单词?即使第二行变长并且不显示也是如此。

我在 javascript section and another one in ruby section 上阅读了一个类似的问题,但我无法将给定的答案转化为我的 Python 案例。

使用textwrap模块:

>>> textwrap.wrap("This text will display on 3 LCD lines", 20)
['This text will', 'display on 3 LCD', 'lines']

使用生成器:

LCDCHARS = 20
LINE = "This text will display on 2 LCD lines No more!"
LCDLINES = 2

def split_line(line):
    words = line.split()                                                                                                                               
    l = ""
    # Number of lines printed
    i = 0
    for word in words:
        if i < LCDLINES - 1 and len(word)+ len(l) > LCDCHARS:
            yield l.strip()
            l = word
            i += 1
        else:
            l+= " " + word
    yield l.strip()

for line in split_line(LINE):
    print line

输出:

This text will
display on 2 LCD lines No more!
YOUR_STRING = "This text will display on 10 LCD lines"
CHAR_LIMIT = 25 # anything

首先,让我们从找出断点(在你的例子中是空格)开始。

让我们使用

中的函数
def find(s, ch):
    return [i for i, ltr in enumerate(s) if ltr == ch]

breakpoints = find(YOUR_STRING, " ")
# [4, 9, 14, 22, 25, 28, 32]

现在,我们找到单词的索引是什么,直到我们可以安全地拆分句子。

让我们从中找到另一个函数:

def element_index_partition(points, breakpoint):
    return [ n for n,i in enumerate(points) if i>breakpoint ][0]

best = element_index_partition(breakpoints, CHAR_LIMIT)

现在,我们只需要拆分并重新连接字符串。

# We won't go till `best` (inclusive) because the function returns the next index of the partition
first_str = " ".join(YOUR_STRING.split(" ")[:best])
last_str =  " ".join(YOUR_STRING.split(" ")[best:])

编辑 在看到 Dan D. 给出的答案后,使用那个答案。始终使用库,而不是无力地尝试重新发明轮子。总是。