0

I have a big string like this

[31m\n[1m[31mError: [0m[0m[1mError import KeyPair: InvalidKeyPair.Duplicate: The keypair 'avtx-ctrl-key' already exists.\n\tstatus code: 400, request id: a56bb69b-0dc1-4c2f-a0c0-96d1be7c1efe[0m\n\n[0m[0m[0m\n[31m\n[1m[31mError: [0m[0m[1mError creating IAM Role my-role-app: EntityAlreadyExists: Role with name my-role-app already exists.\n\tstatus code: 409, request id: a3cbed94-1e30-4cdd-b125-548d422ba105[0m\n\n[0m[0m[0m\n[31m\n[1m[31mError: [0m[0m[1mError creating IAM policy my-assume-role-policy: EntityAlreadyExists: A policy called my-assume-role-policy already exists. Duplicate names are not allowed.\n\tstatus code: 409, request id: 643d1d64-4b27-43db-be11-5585410c31c2[0m\n\n[0m[0m[0m\n[31m\n[1m[31mError: [0m[0m[1mError creating IAM Role my-role-ec2: EntityAlreadyExists: Role with name my-role-ec2 already exists.\n\tstatus code: 409, request id: a741fc5f-7638-4489-be09-6f97dddf148d[0m\n\n[0m[0m[0m\n[31m\n[1m[31mError: [0m[0m[1mError creating VPC: VpcLimitExceeded: The maximum number of VPCs has been reached.\n\tstatus code: 400, request id: 0b8e7173-cd27-42d1-8ae1-a872035a8dcd[0m\n\n[0m[0m[0m\n[31m\n[1m[31mError: [0m[0m Controller launch failed, aborting.\n

Now I want to get sub strings from it like this :

1. Error import KeyPair: InvalidKeyPair.Duplicate: The keypair 'avtx-ctrl-key' already exists.status code: 400,
2. Error: Error creating IAM policy my-assume-role-policy: EntityAlreadyExists: A policy called my-assume-role-policy already exists. Duplicate names are not allowed.\n\tstatus code: 409, request id: 643d1d64-4b27-43db-be11-5585410c31c2

and so on.

Explanation : Evvery sub string will be extracted which start from Word Error and end where request id of that error is show till then. So same way every string can have 4,5 sub strings.

1 Answers1

0

Looks like you're getting your output with mixed ANSI terminal character codes.

First thing to do is to remove ANSI escape sequences. Look at this answer for "How can I remove the ANSI escape sequences from a string in python".

After removing the escape secuences, you should get a str that you can split with str.split()

Iñigo González
  • 3,735
  • 1
  • 11
  • 27