[英]Looping through the list python aws
I am new to python and trying to figure this out for long.我是 python 的新手,并试图长时间解决这个问题。
a ='''arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/**abc**/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/def/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/ghi/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/test/888888888888888'''
print(a)
How to loop through the only string如何遍历唯一的字符串
'arn:aws:elastic:us-west-:111111111111:loadbalancer/app/**abc**/888888888888888'
from the list without specifying in the code even if it's position changes?从列表中而不在代码中指定,即使它的位置发生变化?
Please explain!请解释! Thank you!谢谢!
This code should do the trick:这段代码应该可以解决问题:
for line in a.split():
print(line)
And if you want to find the line you mentioned:如果你想找到你提到的那一行:
for line in a.split():
if line == 'arn:aws:elastic:us-west-:111111111111:loadbalancer/app/**abc**/888888888888888':
print(line)
You can split a string with the methode split
您可以使用拆分方法split
字符串
a ='''arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/**abc**/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/def/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/ghi/888888888888888
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/test/888888888888888'''
for i, split_txt in enumerate(a.split()):
print(f'{i+1}. string: {split_txt}')
Output输出
1. string: arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/**abc**/888888888888888
2. string: arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/def/888888888888888
3. string: arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/ghi/888888888888888
4. string: arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/test/888888888888888
If you only want the first line如果你只想要第一行
a.split()[0]
'arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/**abc**/888888888888888'
If you want to find the line with different pattern如果你想找到不同图案的线条
for i in a.split():
if '**abc**' in i:
print(i)
arn:aws:elastic:us-west-1:111111111111:loadbalancer/app/**abc**/888888888888888
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.