使用python识别段落中的第一个句子

2024-06-25 05:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python从段落中得到第一句话。 该段如下

ECONOMYNEXT-斯里兰卡卢比兑美元周五稳定收报176.40/50卢比,金边收益率小幅走高,因二级市场获利回吐,尽管央行下调政策利率以恢复信贷需求,但市场参与者表示,股市下跌0.26%。

周四现货市场卢比兑美元收于176.40/50卢比。

我是从下面写的代码中提取句子直到 小数点。谢谢你的帮助

import requests
#from pprint import pprint
from IPython.display import HTML
import json


txt = ''' ECONOMYNEXT -Sri Lanka rupee closed steady at 176.40/50 rupees to the US dollar on Friday and gilt yields edged higher on profit taking in the secondary market even as the Central Bank cut policy rates to revive credit demand, while stocks ended 0.26 percent lower, market participants said.

The rupee ended at 176.40/50 rupees against the greenback in the spot market on Thursday. '''

if len(txt) > 100:
    txt = txt.partition('.')[0] + '.'
print(txt)

Tags: thetoinfromimporttxt市场on
2条回答

你可以试试这个

txt = " ECONOMYNEXT -Sri Lanka rupee closed steady at 176.40/50 rupees to the US 
        dollar on Friday and gilt yields edged higher on profit taking in the 
        secondary market even as the Central Bank cut policy rates to revive credit 
        demand, while stocks ended 0.26 percent lower, market participants said. The 
        rupee ended at 176.40/50 rupees against the greenback in the spot market on 
        Thursday. "

sentence_index = txt.find('. ')

print(txt[0: sentence_index])

您将得到如下输出

ECONOMYNEXT -Sri Lanka rupee closed steady at 176.40/50 rupees to the US dollar on Friday and gilt yields edged higher on profit taking in the secondary market even as the Central Bank cut policy rates to revive credit demand, while stocks ended 0.26 percent lower, market participants said

尝试用'. '(用空格)和'.\n'分割

相关问题 更多 >