Python3打印“ ENC [PKCS7”和“]”之间的所有内容

时间:2019-07-06 04:24:45

标签: python string file text

我正在从命令输出中抓取两个哈希。两个哈希均以ENC [BKSC8和in in]开头。在命令输出期间,将显示两个哈希。但是它们以不同的格式显示。命令输出如下所示。这只是一个示例哈希:

string: ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdf;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfd]

OR

block: >
    ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkal
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    asdlad;fljasd;lfjasdjfl;kjal]

我尝试将命令输出放入循环并分割字符串,但是由于命令输出的格式化方式不起作用。

cmd = "my encryption command --BKSC8 my-file" (Just command example)
output = subprocess.getoutput(cmd)

for line in subprocess.getoutput(cmd).splitlines():
    if "[" and "]" in line:
        print(line)

我正在尝试以相同的格式输出两个哈希值。但是只有两个哈希值。像下面这样,并保持相同的格式。

string: ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdf;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfl;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkajsdl;fjal;sdkjfajsfalsdfd]

OR

block: >
    ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkal
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    asdlad;fljasd;lfjasdjfl;kjal]

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用正则表达式模式?我需要更多地了解命令输出可能还会产生什么,但是这种模式似乎适用于您提供的示例。

pip3 install iptcinfo3,其中hashes = re.findall(r"ENC\[BKSC8[^\[]+\]",text)是命令的输出。 text是包含哈希的列表。示例:

hashes

产生以下输出:

import re

text = """
Junk
not stuff we want
faksdfjaksdlfjasdk;jf
123412341234
ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkal
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    asdlad;fljasd;lfjasdjfl;kjal]
more junk
more junk
block: >
    ENC[BKSC8,l;kasjdfj;asldkfa;sdfja;dsjf;ajdskfjaksdfa;sldjfkal
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    ;alsdjf;lasdjfljdlf;alsdfja;lsdfj;alsdjf;alsdkjf;laskdjf;lak
    asdlad;fljasd;lfjasdjfl;kjal]
"""

hashes = re.findall(r"ENC\[BKSC8[^\[]+\]",text)
for hash in hashes:
    print(hash)
    print("------------------")

答案 1 :(得分:0)

使用Jacob Rodal的建议非常适合我尝试做的事情。我能够使用re模块和Jacob建议的命令存储两个哈希。