大块段落

时间:2019-06-22 04:39:00

标签: python

我的弦很大(正好是4个英文段落)。我想将其分成n个字符串块,在连接时应形成相同的三个段落。

text =“段落是将一组句子组合在一起并与一个主题相关的许多句子。或者,一组相关的句子形成了一个点。\ n \ 该定义表明,作品的段落不只是任意的划分。必须根据所引入观念的变化将一章分为几段。\ n \ 因此,没有关于段落长度的规则。根据情况的不同,时间可以短也可以长。一个段落可以包含一个句子或多个句子。 \ n \ 在这方面,散文的段落不同于一首诗的诗节。一首诗的节通常具有相同的长度和样式,但根据要在每个标题下表示的事物的数量,段落的长度可以是长或短。\ n“

3 个答案:

答案 0 :(得分:1)

给出string是该段, 分割段落:

paragraphs = string.split('\n')

要加入结果列表,请执行以下操作:

string = '\n'.join(paragraphs)

答案 1 :(得分:0)

1)。从“ \ n”中分割字符串。拆分后,您将获得一个列表。

para = string.split("\n")

2)。您可以借助.join()

将该列表转换为字符串。
new_string = "\n".join(para)

答案 2 :(得分:0)

如果你想自由就没有。您可以执行一堆字符串

    text = " A paragraph is a number of sentences grouped together and relating to one topic. Or, a group of related sentences that develop a single point.\n \ This definition shows that the paragraphs of compositions are not mere arbitrary divisions. The division of a chapter into paragraphs must be made according to the changes of ideas introduced.\n \ There is, therefore, no rule as to the length of a paragraph. It may be short or long according to the necessity of the case. A paragraph may consist of a single sentence or of many sentences. \n \ In this aspect, the paragraphs of a piece of prose differ from the stanzas of verses of a poem. The stanza of a poem are usually of the same length and pattern but paragraphs are long or short according to the amount of matter to be expressed under each head.\n"

def split_n_times(n):
    l=[]
    for i in range(0,len(text),n):
        l.append(text[i:n+i])
    return l

def join_n_times(l):
    s=''
    for i in l:
        s+=i
    return repr(s)


if __name__ == '__main__':
    l=split_n_times(7)
    print(l)
    print("\n\n-----------------------------------------------")
    print(join_n_times(l))

现在输出为:

    [' A para', 'graph i', 's a num', 'ber of ', 'sentenc', 'es grou', 'ped tog', 'ether a', 'nd rela', 'ting to', ' one to', 'pic. Or', ', a gro', 'up of r', 'elated ', 'sentenc', 'es that', ' develo', 'p a sin', 'gle poi', 'nt.\n \\ ', 'This de', 'finitio', 'n shows', ' that t', 'he para', 'graphs ', 'of comp', 'osition', 's are n', 'ot mere', ' arbitr', 'ary div', 'isions.', ' The di', 'vision ', 'of a ch', 'apter i', 'nto par', 'agraphs', ' must b', 'e made ', 'accordi', 'ng to t', 'he chan', 'ges of ', 'ideas i', 'ntroduc', 'ed.\n \\ ', 'There i', 's, ther', 'efore, ', 'no rule', ' as to ', 'the len', 'gth of ', 'a parag', 'raph. I', 't may b', 'e short', ' or lon', 'g accor', 'ding to', ' the ne', 'cessity', ' of the', ' case. ', 'A parag', 'raph ma', 'y consi', 'st of a', ' single', ' senten', 'ce or o', 'f many ', 'sentenc', 'es. \n \\', ' In thi', 's aspec', 't, the ', 'paragra', 'phs of ', 'a piece', ' of pro', 'se diff', 'er from', ' the st', 'anzas o', 'f verse', 's of a ', 'poem. T', 'he stan', 'za of a', ' poem a', 're usua', 'lly of ', 'the sam', 'e lengt', 'h and p', 'attern ', 'but par', 'agraphs', ' are lo', 'ng or s', 'hort ac', 'cording', ' to the', ' amount', ' of mat', 'ter to ', 'be expr', 'essed u', 'nder ea', 'ch head', '.\n']


-----------------------------------------------
' A paragraph is a number of sentences grouped together and relating to one topic. Or, a group of related sentences that develop a single point.\n \\ This definition shows that the paragraphs of compositions are not mere arbitrary divisions. The division of a chapter into paragraphs must be made according to the changes of ideas introduced.\n \\ There is, therefore, no rule as to the length of a paragraph. It may be short or long according to the necessity of the case. A paragraph may consist of a single sentence or of many sentences. \n \\ In this aspect, the paragraphs of a piece of prose differ from the stanzas of verses of a poem. The stanza of a poem are usually of the same length and pattern but paragraphs are long or short according to the amount of matter to be expressed under each head.\n'
相关问题