String to array in python (boto3)

Viewed 259

I have a code that results in multiple string objects and I want to convert them into an array. The end result looks like this

Queue1

Queue2

Queue3

but, I need it like this

[Queue1, Queue2, Queue3]

P.S. I am new to programming

import boto3
import numpy

rg = boto3.client('resource-groups')
cloudwatch = boto3.client('cloudwatch')

    
#def queuenames(rg):    
response = rg.list_group_resources(
    Group='env_prod'
)
resources = response.get('Resources')
for idents in resources:
    identifier = idents.get('Identifier')
    resourcetype = identifier.get('ResourceType')
    if resourcetype == 'AWS::SQS::Queue':
        RArn = identifier.get('ResourceArn')
        step0 = RArn.split(':')
        step1 = step0[5]
        print(step1)
2 Answers

To convert a string to a list do this:

arr = 'Queue1 Queue2 Queue3'.split(' ')

# Result:
['Queue1', 'Queue2', 'Queue3']

You have a cycle where upon each step you print a string. Try creating an array before the cycle and adding each string inside the cycle, like this (I'm not fluent in Python, please excuse me if there is something wrong in the syntax)

import boto3
import numpy

rg = boto3.client('resource-groups')
cloudwatch = boto3.client('cloudwatch')

    
#def queuenames(rg):    
response = rg.list_group_resources(
    Group='env_prod'
)
resources = response.get('Resources')
myArray = []
for idents in resources:
    identifier = idents.get('Identifier')
    resourcetype = identifier.get('ResourceType')
    if resourcetype == 'AWS::SQS::Queue':
        RArn = identifier.get('ResourceArn')
        step0 = RArn.split(':')
        step1 = step0[5]
        print(step1)
        myArray.append(step1)

The code above will not change the way your output is displayed, but builds the array you need. You can remove the print line and print the array after the cycle instead.

Related