Copie d'un groupe de mot en python

Tags: #<Tag:0x00007fb98899ddf8>

Bonsoir,

Je cherche à copier une partie du contenu de fichier 1 (user02.crt) vers fichier 2 (data.txt).

Voici mon fichier 1 dont je souhaite copier le contenu (et plus précisément de BEGIN CERTIFICATE à END CERTIFICATE) :

Certificate:
Data:
    Version: 3 (0x2)
    Serial Number:
        c3:97:c6:d9:82:61:dd:1d:04:6d:01:a4:31:d0:e7:c3
    Signature Algorithm: sha256WithRSAEncryption
    Issuer: CN=ENTREPRISE.LAN
    Validity
        Not Before: Dec  8 22:23:21 2021 GMT
        Not After : Mar 12 22:23:21 2024 GMT
    Subject: CN=user02
    Subject Public Key Info:
        Public Key Algorithm: rsaEncryption
            RSA Public-Key: (2048 bit)
            Modulus:
                00:d3:89:1f:eb:e7:dd:6e:eb:f8:b5:14:48:26:61:
                57:56:49:a6:ee:91:22:b3:12:d1:42:b0:d3:00:e0:
                f5:7b:95:d2:b3:64:8e:f4:d2:50:af:6d:7e:2d:34:
                95:2c:a5:8d:01:b7:8f:c6:b8:9b:3f:38:cc:3c:45:
                d1:ae:5f:31:fe:b9:81:a2:81:2b:c7:75:9c:43:de:
                8c:56:f2:c4:83:68:60:e5:e6:47:21:f6:b8:16:a0:
                4d:28:9f:2b:b2:ff:70:e8:e9:bf:59:c1:ed:f6:20:
                09:e5:47:49:3c:d6:8c:ee:53:ea:85:2b:d5:6c:f9:
                94:7d:6b:b9:11:52:17:9f:80:fe:c9:30:bd:e4:f6:
                ef:02:43:1f:15:55:22:06:67:0b:e2:a8:1f:c1:0b:
                42:40:4b:09:1a:df:24:b7:90:78:d6:82:c6:88:46:
                d5:8d:69:c2:17:21:76:3c:69:e8:18:d8:83:4a:a5:
                31:62:88:41:27:8d:41:3d:57:b0:b2:8b:67:1c:6a:
                d5:eb:9b:54:ba:ef:6e:1d:d7:12:d4:46:ae:32:bd:
                94:da:2e:b4:9e:1f:f7:11:48:ba:d0:3d:b4:1e:3d:
                75:9c:55:ee:6a:b8:09:07:be:a2:fd:53:0f:35:9c:
                09:a1:c8:30:aa:d9:cc:7f:e7:d3:e2:7f:a4:03:e2:
                6e:63
            Exponent: 65537 (0x10001)
    X509v3 extensions:
        X509v3 Basic Constraints: 
            CA:FALSE
        X509v3 Subject Key Identifier: 
            4E:27:AD:C3:C3:9D:3D:4C:29:9C:DF:AA:B2:10:56:58:23:DF:ED:1F
        X509v3 Authority Key Identifier: 
            keyid:1D:F7:98:81:B5:36:32:01:1A:2C:AC:EE:47:CF:C9:04:C6:4B:3A:89
            DirName:/CN=ENTREPRISE.LAN
            serial:2A:5D:31:07:E3:41:6C:ED:9F:A6:33:97:EB:A1:17:2E:5A:F6:C6:63

        X509v3 Extended Key Usage: 
            TLS Web Client Authentication
        X509v3 Key Usage: 
            Digital Signature
Signature Algorithm: sha256WithRSAEncryption
     13:94:ce:4c:65:eb:f8:15:e3:00:52:67:cf:5f:53:22:5b:0f:
     3f:42:e2:5d:cc:56:e6:a5:db:dd:85:eb:4e:39:a3:ab:7b:24:
     43:89:ed:a1:2e:79:28:31:15:da:83:9a:dc:9c:77:be:ec:02:
     24:2d:60:c5:fd:21:36:5a:db:6d:8a:49:44:70:b8:be:3e:68:
     07:b0:f7:a1:b8:78:84:04:0e:f1:e9:85:1a:52:f5:61:b9:e8:
     de:11:fe:60:20:fd:34:9d:e0:77:2a:77:23:bf:3d:f6:82:3b:
     a9:3e:1e:13:00:1b:cb:12:c7:95:23:ba:07:f9:47:18:85:8b:
     66:4b:a5:fa:d5:08:9c:2f:7b:fa:e9:09:51:77:c6:28:e5:1f:
     4d:a2:0e:fc:2f:bb:af:6d:e6:47:d0:f9:cd:e3:dd:05:c6:13:
     05:45:da:bd:56:92:ad:07:f5:6f:29:db:c5:f5:43:27:a1:81:
     87:08:4e:98:95:df:0b:9e:2a:16:e4:1a:64:81:c4:02:ac:01:
     91:d7:06:4c:e0:1b:96:a3:e6:cc:d6:07:90:7e:a5:30:ec:18:
     54:69:f3:03:0e:3f:52:4e:75:2f:ba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8sSx5Ujugf5RxiFi2ZLpfrVCJwve/rpCVF3xijlH02iDvwvu69t5kfQ+c3j3QXG
EwVF2r1Wkq0H9W8p28X1QyehgYcITpiV3wueKhbkGmSBxAKsAZHXBkzgG5aj5szW
B5B+pTDsGFRp8wMOP1JOdS+6RXdmW3LH2U0GQR14q9jbDyIxx9kxSdEFds9OWqg=
-----END CERTIFICATE-----

Mon petit script python :

fichier1 = open("user02.crt", "r")
MonTexte = fichier1.read()
fichier1.close()

fichier2 = open("data.txt", "a")
fichier2.write(MonTexte)
fichier2.close()

Comment procéder en ajustant mon script ? Vers quelle direction m’orienter ?

Merci bien

Clément

Pour commencer, utilise le mot clé with.

with open("user02.crt", "r") as entrée:
    with open("data.txt","a") as sortie:
        dedans=False
        for ligne in entrée:
            if ligne.startswith("-----BEGIN CERTIFICATE-----"):
                    dedans=True
                    continue
            if ligne.startswith("-----END CERTIFICATE-----"):
                    dedans=False
                    continue
            if dedans:
                sortie.write(ligne)

Je n’ai pas testé si deux with emboîtés fonctionnent mais ça devrait.

1 J'aime

Testé, ça fonctionne.

J’ai ajouté sortie.write("-----BEGIN CERTIFICATE-----\n") et sortie.write("-----END CERTIFICATE-----") dans le script pour inclure le BEGIN et le END.

Tu viens d’illuminer ma journée. merci !