简体   繁体   English

在Python中打印正则表达式的结果

[英]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. 我需要搜索用户创建的AWS策略,以查看其中是否包含带有用户名的策略。

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? 如何正确执行此操作?

What you are searching in is a dictionary or a json object not a string . 您要搜索的是dictionary or a json object而不是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 . response 不是字符串,因此您不能使用re搜索它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM