Skip to content
Advertisement

If statement filtering with a * character inside the value

I’m iterating a dataframe with the following code:

print(access_df)

Out:
                        ActionAccess        Arn
0                               s3:*        a--...
1                       xray:PutTele        b--...
2               s3:*MultipartUpload*        c--...
3            xray:GetSamplingTargets        d--...

for index, row in access_df.iterrows():
    if 's3:*' in row['ActionAccess']:
        print(row)

I’m trying to get values that are only s3:*. However the above code also detects cases such as s3:*Object and s3:*MultipartUpload*.

How do I define the if statement to only get values that contain only s3:*?

Advertisement

Answer

Not sure if I really understand the question, but if you just want to check whether the string is literally 's3:*', i.e. the * is not supposed to be some kind of placeholder/wildcard, just use ==:

if 's3:*' == row['ActionAccess']:

If you are looking for 's3:*' (but not 's3:*stuff*') as part of a larger string, e.g. "some string with s3:* inside", then you might use str.split or resort to a regular expression.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement