使用python

时间:2019-06-05 10:15:26

标签: python python-2.7 text

我想使用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)

2 个答案:

答案 0 :(得分:1)

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

分割

答案 1 :(得分:1)

您可以尝试...

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-斯里兰卡卢比兑美元汇率周五稳定收于176.40 / 50卢比,而二级市场的获利回吐使金边债券收益率微升,尽管中央银行降低了政策利率以恢复信贷需求,而股票收于0.26市场参与者说,价格下降了百分之