2

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
  • So what exactly is the problem? Are you getting an error? The wrong results? Commented May 6, 2021 at 9:24
  • I need an array to continue into further coding. But, The result I am getting is a string. Commented May 6, 2021 at 9:49

2 Answers 2

4

To convert a string to a list do this:

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

# Result:
['Queue1', 'Queue2', 'Queue3']
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, the problem is I won't print the queue names. So there is no way to mention the names and use split. If I use split on step1 itself, the result are "lists" and it looks like this. ['Queue2'] ['Queue1'] ['myQueue']
The response from Lajos seems to be more what you're looking for, I was just responding to your question doesn't respond to your needs.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.