在Python 3中每次输入后打印换行符

时间:2019-02-25 18:31:05

标签: python python-3.x

我想用以下代码做三件事。但是我刚接触python 3(和一般的python)。

  • 删除开头和结尾的括号
  • 删除逗号
  • 将每个条目放在自己的行上

这是到目前为止的代码:

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)

这是上面代码的输出:

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']

我该如何实现这三件事?

1 个答案:

答案 0 :(得分:4)

使用列表分解和print的{​​{1}}指示符将sep放在每个元素打印图之间:

'\n'

输出:

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")

Doku: