我有一个代码,它是用来在20x2液晶显示器上显示一些文本的:
#!/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))
将输出示例字符串
^{pr2}$我该怎么做才能在不破坏单词的情况下拆分字符串?即使第二行变长并退出显示。在
我在javascript section上读到了一个类似的问题,而在ruby section中又读到了另一个问题,但我无法将给出的答案翻译成我的Python案例。在
首先,让我们先找出断点(在您的例子中是空格)。在
让我们使用来自https://stackoverflow.com/a/11122355/2851353的函数
^{pr2}$现在,我们找到这个词的索引,直到我们可以安全地拆分句子为止。在
让我们从中找到另一个函数:https://stackoverflow.com/a/2236956/2851353
现在,我们只需要分开并重新连接绳子。在
编辑 在看到Dan D给出的答案后,使用该答案。总是使用库,而不是做一些无力的尝试来重新发明轮子。总是这样。在
使用发电机:
输出:
^{pr2}$使用^{} 模块:
相关问题 更多 >
编程相关推荐