如何根据句点或换行符出现的位置将文本拆分为句段?



我的文字是:

'3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEWnThe initial term of this Lease shall be for a period of Five (5) years commencing on'

我想要以下列表:

[
'3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEW',
'The initial term of this Lease shall be for a period of Five (5) years commencing on'
]
>>> initial_statement = "3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEWnThe initial term of this Lease shall be for a period of Five (5) years commencing on"
>>> temp_list = initial_statement.split("n")
>>> print(temp_list)
['3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEW', 'The initial term of this Lease shall be for a period of Five (5) years commencing on.']

或者再次将其设置为字符串

>>> final_result = ""
>>> for i in temp_list:
...     final_result += i
... 
>>> print(final_result)
3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEWThe initial term of this Lease shall be for a period of Five (5) years commencing on.

如果将.的所有实例替换为n,则可以在每次n拆分,并获得所需的输出。


text = '3. COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEWnThe initial term of this Lease shall be for a period of Five (5) years commencing on'
# You might wish to replace the "." with a ".n" instead so the "." is preserved
text = text.replace(".","n")
text = text.split("n")
print(text)

输出:

['3', ' COMMENCEMENT; TERM OF LEASE; AND OPTION TO RENEW', 'The initial term of this Lease shall be for a period of Five (5) years commencing on']

最新更新