python提取子串_从字符串Python提取子字符串

如果我正确理解您的意见,然后拆分应该采取的措施:

if "$Revision:" in line:

print(line.split("$Revision: ")[1].split()[0])

1.14

In [6]: line ="""

...: -- CVS Header: $Source: /CVS/oracle11i/database/erp/apps/pkgspec/wwt_prime_pkg.p

...: ls,v $, $Revision: 1.14 $, $Author: $, $Date: 2014/09/23 21:41:15 $

...: """

In [7]: line.split("$Revision: ") # split the line at $Revision:

Out[7]:

['\n-- CVS Header: $Source: /CVS/oracle11i/database/erp/apps/pkgspec/wwt_prime_pkg.p\nls,v $, ',

'1.14 $, $Author: $, $Date: 2014/09/23 21:41:15 $\n']

# we use indexing to get the first element after $Revision: in the string

In [8]: line.split("$Revision: ")[1]

# which becomes the substring below

Out[8]: '1.14 $, $Author: $, $Date: 2014/09/23 21:41:15 $\n'

# if we call split again we split that substring on whitespace into individual strings

In [10]: '1.14 $, $Author: $, $Date: 2014/09/23 21:41:15 $\n'.split()

Out[10]: ['1.14', '$,', '$Author:', '$,', '$Date:', '2014/09/23', '21:41:15', '$']

# using indexing again we extract the first element which is the revision number

In [11]: '1.14 $, $Author: $, $Date: 2014/09/23 21:41:15 $\n'.split()[0]

Out[11]: '1.14'

$Date相同:

date = line.split("$Date: ")[1].split()[0]

或者,如果您只想检查字符串中的子字符串,请使用in:

if "$Revision: 1.14" in line:

print line

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值