【发布时间】:2021-10-19 10:23:42
【问题描述】:
我有一个文本文档,只要出现\n 或.,就必须拆分它。使用split(),我们可以拆分,但要根据\n 和. 来实现。
代码
text = 'Christmas Perot 2021 TSO\nSkip to Main Content HOME CONCERTS EVENTS ABOUT STAFF EDUCATION SUPPORT US More Use tab to navigate through the menu items. BUY TICKETS SUNDAY, DECEMBER 12, 2021 I PEROT THEATRE I 4:00 PM\nPOPS I Christmas at The Perot\nCLICK HERE to purchase tickets, or contact the Texarkana Symphony Orchestra at 870.773.3401\nA Texarkana Tradition Join the TSO, the Texarkana Jazz Orchestra, and the TSO Chamber Singers, for this holiday concert for the whole family.\nDon’t miss seeing the winner of TSO’s 11th Annual Celebrity Conductor Competition\nBack to Events 2019 Texarkana Symphony Orchestra'
sentences = text.split('\n')
print(sentences)
输出
['Christmas Perot 2021 TSO',
'Skip to Main Content HOME CONCERTS EVENTS ABOUT STAFF EDUCATION SUPPORT US More Use tab to navigate through the menu items. BUY TICKETS SUNDAY, DECEMBER 12, 2021 I PEROT THEATRE I 4:00 PM',
'POPS I Christmas at The Perot',
'CLICK HERE to purchase tickets, or contact the Texarkana Symphony Orchestra at 870.773.3401',
'A Texarkana Tradition Join the TSO, the Texarkana Jazz Orchestra, and the TSO Chamber Singers, for this holiday concert for the whole family.',
'Don’t miss seeing the winner of TSO’s 11th Annual Celebrity Conductor Competition',
'Back to Events 2019 Texarkana Symphony Orchestra']
期望的输出
['Christmas Perot 2021 TSO',
'Skip to Main Content HOME CONCERTS EVENTS ABOUT STAFF EDUCATION SUPPORT US More Use tab to navigate through the menu items.',
'BUY TICKETS SUNDAY, DECEMBER 12, 2021 I PEROT THEATRE I 4:00 PM',
'POPS I Christmas at The Perot',
'CLICK HERE to purchase tickets, or contact the Texarkana Symphony Orchestra at 870.773.3401',
'A Texarkana Tradition Join the TSO, the Texarkana Jazz Orchestra, and the TSO Chamber Singers, for this holiday concert for the whole family.',
'Don’t miss seeing the winner of TSO’s 11th Annual Celebrity Conductor Competition',
'Back to Events 2019 Texarkana Symphony Orchestra']
【问题讨论】:
-
您可以用换行符替换所有点,然后进行拆分;交替用换行符分割,并为分割中的每个元素按点分割并将结果收集到新数组中
-
870.773.3401 有“.”