2017-12-16 3 views
0

ich so einen Beispieltext haben,Split Text von Absatz

## Paragraph 1\n\nThe [`sys`](https://docs.python.org/3.6/library/sys.html#module-sys) module also has attributes for *stdin*, *stdout*, and *stderr*. \n\nThe latter is useful for emitting warnings and error messages to make them visible even when *stdout* has been redirected:\n\n## Paragraph 2\n\nThe [`re`](https://docs.python.org/3.6/library/re.html#module-re) module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:\n\nWhen only simple capabilities are needed, string methods are preferred because they are easier to read and debug. 

Ich mag würde es zwei Absätze aufgeteilt regex statt str.split, so habe ich versucht.

In [18]: para = re.findall(r'## .+', content) 
In [19]: para 
Out[19]: ['## Paragraph 1', '## Paragraph 2'] 

Der Ausgang I Absicht ist separater vollständiger Absatz.

['## Paragraph 1\n\nThe [`sys`](https://docs.python.org/3.6/library/sys.html#module-sys) module also has attributes for *stdin*, *stdout*, and *stderr*. \n\nThe latter is useful for emitting warnings and error messages to make them visible even when *stdout* has been redirected:\n\n', 
'## Paragraph 2\n\nThe [`re`](https://docs.python.org/3.6/library/re.html#module-re) module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:\n\nWhen only simple capabilities are needed, string methods are preferred because they are easier to read and debug.'] 

Wie wird das erreicht?

+0

Welche Sprache verwenden Sie? Gewöhnlich gibt es eine eingebaute String-Funktion zum Aufteilen von Strings, z.B. 'split()' in JavaScript oder 'explode()' in PHP. –

+0

Ich füge Python als Tag hinzu, danke. @ ssc-hrep3 – Computing

Antwort

2

Sie können dies versuchen:

import re 
s = " ## Paragraph 1\n\nThe [`sys`](https://docs.python.org/3.6/library/sys.html#module-sys) module also has attributes for *stdin*, *stdout*, and *stderr*. \n\nThe latter is useful for emitting warnings and error messages to make them visible even when *stdout* has been redirected:\n\n## Paragraph 2\n\nThe [`re`](https://docs.python.org/3.6/library/re.html#module-re) module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:\n\nWhen only simple capabilities are needed, string methods are preferred because they are easier to read and debug." 
paragraphs = re.split('\n(?=## Paragraph \d+)', s) 

Ausgang:

[' ## Paragraph 1\n\nThe [`sys`](https://docs.python.org/3.6/library/sys.html#module-sys) module also has attributes for *stdin*, *stdout*, and *stderr*. \n\nThe latter is useful for emitting warnings and error messages to make them visible even when *stdout* has been redirected:\n', 
'## Paragraph 2\n\nThe [`re`](https://docs.python.org/3.6/library/re.html#module-re) module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:\n\nWhen only simple capabilities are needed, string methods are preferred because they are easier to read and debug.'] 
0

Sie können versuchen, die eingebaute Split-Funktion

string = '''I am new to python # please help me ''' 
data = string.split('#') 
print(data) 

Ausgabe

[ 'Ich bin neu in Python', 'Bitte helfen Sie mir']