从python的命令输出中提取一行代码

时间:2019-06-24 11:23:21

标签: python

我有如下命令输出,其中包含加密代码块。如何提取block: >下的内容并将其分配给变量。必须在变量的所有行之前保留空格。由于我是python的新手,所以不确定如何继续。

HlOJsN8A2NSOQGAmhKy30qi7qiIE6qponUZmwJ6fUzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA11C/9cV/fOIrO6VZGzIM69rguMSGObeUKLgDledCf1mFii8WhCA8EChnN3hh4M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz7TH6gjA8BgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBA84gFtQxzw2+2Lofkpt/q6gBBA9Y/R7C0tFSl2FxfDUa/I]

OR

block: >
    ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
    DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
    fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
    UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
    3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
    fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
    M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
    vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
    t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]

必填输出

some_var="ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
    DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
    fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
    UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
    3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
    fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
    M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
    vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
    t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]"

1 个答案:

答案 0 :(得分:0)

在python中,如果不想在字符串中看到"""multi_line_str""""\n",则应使用"\t"

origin_str = """block: >
    ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
    DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
    fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
    UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
    3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
    fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
    M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
    vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
    t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]"""
what_you_want_str = origin_str.split("block: >\n")[1].strip()
what_you_want_str
'ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw\n    DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP\n    fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f\n    UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk\n    3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1\n    fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh\n    M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz\n    vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh\n    t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]'

print(what_you_want_str)

输出:

ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEw
    DQYJKoZIhvcNAQEBBQAEggEAtcju4aRfjIng9zC/aXQTWYPF/6L8LZufO9GP
    fjhjfhjhjfhjfhjhfjhjhjhfjhjfhjfhjhcagfjgfjfhjkhE6qponUZmwJ6f
    UzFDckBbOMydCui6uY51o91bE4XM8ci+sGqrB6Hvl6JWXASkT7rTLHzpFUTk
    3ii86aR209bZb8nkEsMT4KQEISSHeJ+NAkGZkbnvISWCwWLVv0wLxAW22DA1
    fgjfkhjkfhkjhfjkhjkfhjkhfjkhjkfljlkfhfhkjfhjkhfjkfhjkhfjkhfh
    M5RxyCeZhsVSpy3AbkRnMRN2e4T2ISQqblHb7i4+TU0DoVc4aSnCNlgm4pKz
    vbnbnbjfbjfbjbjfjkhdjhjkfhjkfjkhfjkhjkdhjkhjshwhwjhjkwhjkwhh
    t/q6gBBA9Y/R7C0tFSl2FxfDUa/I]