用最大 len 的较短字符串中的多个定界符拆分一个长字符串

Split one long string by multiple delimeter in shorter string with max len

我尝试将长字符串发送到 raspberry pi 上的 20x4 LCD 显示器,这意味着 4 行 20 个字符。

为了保持液晶显示器上的文本可读,我想拆分

  1. 空格
  2. 特殊字符,如“,”、“.”、“!”、“?”等
  3. 最大行长度为 20
  4. 最大字数为 20

我发现了一些按单词(空格)拆分字符串的代码,但我无法进一步...

s = "This is a very long string with many many many many and many more sentences and there is not one character that i can use to split by, just by number of words 1 2 3 12° C 3, ,5 ,6 .7 5 "
l = s.split()
n = 5
[' '.join(l[x:x+n]) for x in range(0, len(l), n)]

在这种情况下,两个单词可以是 'foobarfoobarfoobar foo',长度超过 20 个字符。

s = "longlongstuff [...]"
n = s.split() #whitespace
n = n.split(",")
n.flatten() #not sure about this.              transforms matrixes (and 3or more dimensional arrays) into one dimensional ones
n = n.split(".")
n.flatten()
#...
for i in range(len(n)):
    e = n[i]
    if len(e) > 20: #length longer than 20
         n[i] = [e[:20],e[19:]]
 n.flatten()
 "\n". join(n)

应该可以。小心理代码

nice textwrap module in the standard library为您带来前90%。它主要根据空格(而不是您的特殊字符)进行拆分。如果你真的也需要拆分这些,你总是可以用一个 whitesapce 作为后缀。

import textwrap
wrapper = textwrap.TextWrapper(width=20)
for line in wrapper.wrap(text):
    print line, len(line)

您可以使用正则表达式在最接近 20 个字符的单词边界处插入换行符:

In [22]: print re.sub(r"(\b.{,20}\b)",r""+"\n",s)
This is a very long 
string with many 
many many many and 
many more sentences 
and there is not one
 character that i 
can use to split by
, just by number of 
words 1 2 3 12° C 3
, ,5 ,6 .7 5 
foobarfoobarfoobar 
foo

如果您想更具体或添加更多逻辑,您还可以将 lambda 传递给 sub :

In [106]: f = lambda x: x.group(1).strip()+",\n"

In [107]: print re.sub(r"(\b.{,20}\b)", f, s).rstrip(",\n")
This is a very long,
string with many,
many many many and,
many more sentences,
and there is not one,
character that i,
can use to split by,
, just by number of,
words 1 2 3 12° C 3,
, ,5 ,6 .7 5,
foobarfoobarfoobar,
foo