Python 如何拆分字符串,使其包含的字符数少于n个(带扭曲)

Python 如何拆分字符串,使其包含的字符数少于n个(带扭曲),python,split,Python,Split,我有一个长字符串要保存到文件中。单词之间用空格隔开。给出了长字符串中的字数可以被3整除 基本上,我在寻找一种将字符串分割成块的方法。每个区块少于n个字符,区块中的字数也可被3整除 e、 g 假设最大线长度为n=30: >>>split_string(longstring, 30) ['This is a very long string', 'and the sum of words is', 'divisible by three'] 总之,这些规则是: 不超过n个字符的行

我有一个长字符串要保存到文件中。单词之间用空格隔开。给出了长字符串中的字数可以被3整除

基本上,我在寻找一种将字符串分割成块的方法。每个区块少于n个字符,区块中的字数也可被3整除

e、 g

假设最大线长度为n=30:

>>>split_string(longstring, 30)
['This is a very long string', 'and the sum of words is', 'divisible by three']
总之,这些规则是:

  • 不超过n个字符的行
  • 一个转折点是每一行必须包含3个单词的倍数
  • 到目前为止,我尝试使用textwrap,但我不知道如何实现2

    import textwrap    
    textwrap.fill(long_line, width=69)
    

    如果确定字符串中的总字数始终可以被3整除,则可以执行以下操作:

    import sys
    #long string; 84 words; divisible by 3
    longString = "The charges are still sealed under orders from a federal judge. Plans were prepared Friday for anyone charged to be into custody as soon as Monday, the sources said. It is unclear what the charges are. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning."
    #convert string to list
    listOfWords = longString.split()
    #list to contain lines
    lines = []
    #make sure number of words is divisible by 3
    if  len(listOfWords) % 3 != 0:
        #exit
        print "words number is not divisible by 3"
        sys.exit()
    #keep going until list is empty
    while listOfWords:
    
        i = 0
        line = ""
        #loop for every line
        while True:
            #puts the next 3 words into a string
            temp = " ".join(listOfWords[i:i+3])
            #check new length of line after adding the new 3 words, if it is still less than 70, add the words, otherwise break out of the loop
            if len(line) + len(temp) > 70:
                break
            line += "{} ".format(temp)
            i+=3
        #remove finished words from the list completely
        listOfWords = listOfWords[i:]
        #adds line into result list
        lines.append(line.strip())
    
    #to make sure this works
    for line in lines:
        print len(str(line))
        print "Number of words: {}".format(len(line.split()))
        print "number of chars: {}".format(len(line))
        print line
        print "----------------------------------------"
    

    一个字符串中的总字数可以被3整除。我会把你的字符串分成几个字,然后针对每个字,测试它是否会使当前句子过长。如果是这样,冻结当前句子,并将该单词作为下一行的开头。
    import sys
    #long string; 84 words; divisible by 3
    longString = "The charges are still sealed under orders from a federal judge. Plans were prepared Friday for anyone charged to be into custody as soon as Monday, the sources said. It is unclear what the charges are. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning. A spokesman for the special counsel's office declined to comment. The White House also had no comment, a senior administration official said Saturday morning."
    #convert string to list
    listOfWords = longString.split()
    #list to contain lines
    lines = []
    #make sure number of words is divisible by 3
    if  len(listOfWords) % 3 != 0:
        #exit
        print "words number is not divisible by 3"
        sys.exit()
    #keep going until list is empty
    while listOfWords:
    
        i = 0
        line = ""
        #loop for every line
        while True:
            #puts the next 3 words into a string
            temp = " ".join(listOfWords[i:i+3])
            #check new length of line after adding the new 3 words, if it is still less than 70, add the words, otherwise break out of the loop
            if len(line) + len(temp) > 70:
                break
            line += "{} ".format(temp)
            i+=3
        #remove finished words from the list completely
        listOfWords = listOfWords[i:]
        #adds line into result list
        lines.append(line.strip())
    
    #to make sure this works
    for line in lines:
        print len(str(line))
        print "Number of words: {}".format(len(line.split()))
        print "number of chars: {}".format(len(line))
        print line
        print "----------------------------------------"