【问题标题】:Splitting sentences froma text based on \n and根据 \n 和从文本中拆分句子
【发布时间】: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']

【问题讨论】:

标签: python string split


【解决方案1】:

一种方法是这样的

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'
semiSentences = text.replace('.','\n').split('\n')
sentences=[]
for s in semiSentences:
    if s.isalnum():
        sentences[-1]=sentences[-1]+'.'+s
    else:
        sentences.append(s)
        
        
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']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多