bluethundr
bluethundr

Reputation: 1345

Print the result of a regex in Python

I need to search through user created AWS policies to see if there is one with a user name in it.

This is the code I'm using:

import re
def create_iam_policy(user_name,aws_account):
     session = boto3.Session(profile_name=aws_account)
     client = session.client('iam')
     response = client.list_policies(Scope='Local',OnlyAttached=False)
     print(str(re.search(user_name, response).group()))

But when I do that I am getting this error:

TypeError: expected string or bytes-like object

How can I do this correctly?

Upvotes: 0

Views: 56

Answers (1)

Samuel Nde
Samuel Nde

Reputation: 2753

What you are searching in is a dictionary or a json object not a string. You might want to change

print(str(re.search(user_name, response).group()))

to

print(re.search(user_name, str(response)).group())

The response is not a string and so you cannot search it using re.

Upvotes: 2

Related Questions