python提取列表元素首字符_Python从列表中提取第一个元素并清除字符串

我正在解析txt文件(共有+100页长),我想提取字符串“public offering price”第一次出现的句子。另外,我想清除句子中的“&nbsp”字符。在

我在一系列文件(文件列表)上运行以下代码:test1 = [] #create a new list to store my desired output

for eachfile in file_list:

with open(eachfile, 'r') as f:

for line in f:

if "public offering price" in line:

test1.append(line.replace(' ','').split('.')[0])

print(test1)

通过上面的代码,我成功地清除了" "字符,并在“.”出现时拆分元素(这对我想要的输出有帮助),但是获得了以下输出:

^{pr2}$

上面的输出给出了所有的句子,包括我想要的字符串,但是我只想保留第一个出现的句子:['public offering price will be between $and $per share]

你知道如何获得这样的产出吗?鉴于我运行的代码,它一定很容易实现,但却不知道如何。。。在

提前非常感谢你

编辑:在不替换或拆分('.')[0]的情况下获得的输出如下:['public offering price will be between $   and $    per share. We intend to apply to list the common stock on\n', 'to    additional shares of our common stock at the initial public offering price.\n', '(2) an initial public offering price of $    per share, the midpoint of the initial public offering range indicated on the cover of this prospectus. \n']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值