Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

python - 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:*?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...