2

I want to do 3 things with the following code. But I'm newb to python 3 (and python in general).

  • Remove the beginning and ending brackets
  • remove the commas
  • put each entry on it's own line

Here is the code so far:

import boto3
# Create an S3 client
s3 = boto3.client('s3')

# Call S3 to list current buckets
response = s3.list_buckets()

# Get a list of all bucket names from the response
buckets = [bucket["Name"] for bucket in response['Buckets']]

# Print out the bucket list
print("Bucket List: %s" % buckets)

Here is my output from the above code:

Bucket List: ['aws-ip-update-01', 'aws-ip-update-02', 'aws-ip-update-bucket', 'case4667772691-cloudtraillogs', 'cf-templates-10gcjonooe0lj-us-east-1', 'config-bucket-832839043616', 'fsaas', 'company-aws-config', 'company-cloudtrail-nonprod', 'company-dev', 'company-isam9', 'company-netbackup-nonprod', 'company-timd-test-bucket', 'kpmgimagedeploy', 'ussvcsplunkaws-nonprod-keys']

How can I achieve these 3 things?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
bluethundr
  • 1,005
  • 17
  • 68
  • 141

1 Answers1

4

Use list decomposition and the print's sep specifier to put a '\n' between each element print prints:

data = ['aws-ip-update-01', 'aws-ip-update-02', 'aws-ip-update-bucket', 
        'case4667772691-cloudtraillogs', 'cf-templates-10gcjonooe0lj-us-east-1', 
        'config-bucket-832839043616', 'fsaas', 'company-aws-config', 
        'company-cloudtrail-nonprod', 'company-dev', 'company-isam9', 
        'company-netbackup-nonprod', 'company-timd-test-bucket', 'kpmgimagedeploy', 
        'ussvcsplunkaws-nonprod-keys']

print("Bucket List: ", *data, sep="\n")

Output:

Bucket List: 
aws-ip-update-01
aws-ip-update-02
aws-ip-update-bucket
case4667772691-cloudtraillogs
cf-templates-10gcjonooe0lj-us-east-1
config-bucket-832839043616
fsaas
company-aws-config
company-cloudtrail-nonprod
company-dev
company-isam9
company-netbackup-nonprod
company-timd-test-bucket
kpmgimagedeploy
ussvcsplunkaws-nonprod-keys

Doku:


Patrick Artner
  • 50,409
  • 9
  • 43
  • 69