获取python中两个重复字符串之间的所有子字符串

时间:2017-03-23 17:17:25

标签: python python-2.7 python-3.x

我有以下内容:

-----BEGIN CERTIFICATE-----
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
MIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCBMIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCBMIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
AgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCBMIIE0zCCA7ugAwIBAgIQGNrRniZ96LtKIVjNzGs7SjANBgkqhkiG9w0BAQUFADCB
-----END CERTIFICATE-----

我希望在----- BEGIN CERTIFICATE -----和----- END CERTIFICATE -----之间获取所有三个字符串,然后将它们存储在python中的变量中。我怎么在python中做到这一点?

1 个答案:

答案 0 :(得分:1)

如果证书存储在一个字符串中,您可以这样做:

start_flag = "-----BEGIN CERTIFICATE-----"
end_flag   = "-----END CERTIFICATE-----"
keys = []
for CERTIFICATE in CERTIFICATES.split( end_flag ):
    key = CERTIFICATE.lstrip( start_flag )
    keys.append( key )

print keys