Python print:字符串格式错误

时间:2017-07-26 20:03:56

标签: python string-formatting

在使用fabric一段时间之后,我一直在尝试将boto3合并到我的AWS工作流程中。刚开始学习Python,所以要提前为其中一些问题道歉。我使用下面的脚本搜索并调试了我可以使用以下脚本,因为大多数错误似乎都是在Python2中编写并且我在OSX上使用Python3。很抱歉格式化问题,并尝试将脚本放入代码块中。

plt1 <- ggplot( plotdata, aes( x = tsne_x, y = tsne_y, color = Cluster ) +
    geom_point() + ggrepel::geom_text_repel( aes( label = SampleID ) )

终端:python ec2-instances.py

并获得以下输出:

    #!/usr/bin/env python
    import boto3
    import sys
    import argparse
    import paramiko


def list_instances(Filter):
    ec2 = boto3.resource('ec2')
    instances = ec2.instances.filter(Filters=Filter)
    columns_format = ("%-3s %-26s %-16s %-16s %-20s %-12s %-12s %-16s")
    print (columns_format) ("num", "Name", "Public IP", "Private IP", "ID", 
    "Type", "VPC", "Status")
    num = 1
    hosts = [] 
    name = {}  
    for i in instances:
        try:
            name = (item for item in i.tags if item["Key"] == "Name" ).next()
        except StopIteration:
            name['Value'] = ''

        print (columns_format) % (
                           num,
                           name['Value'], 
                           i.public_ip_address,
                           i.private_ip_address,
                           i.id,
                           i.instance_type,
                           i.vpc_id,
                           i.state['Name']
                         )
      num = num + 1
      item={'id': i.id, 'ip': i.public_ip_address, 'hostname': 
      name['Value'], 'status': i.state['Name'],}
      hosts.append(item)

  return hosts

def execute_cmd(host,user,cmd):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
       ssh.connect(host, username=user)
       stdin, stdout, stderr = ssh.exec_command(cmd)
       stdout=stdout.read()
       stderr=stderr.read()
       ssh.close()
       return stdout,stderr
    except paramiko.AuthenticationException as exception: 
       return "Authentication Error trying to connect into the host %s with     the user %s. Plese review your keys" % (host, user), e 

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-n', '--name',
                    help="Filter result by name.")
    parser.add_argument('-t', '--type',
                    help="Filer result by type.")
    parser.add_argument('-s', '--status',
                    help="Filter result by status." )
    parser.add_argument('-e', '--execute',
                    help="Execute a command on instances")
    parser.add_argument('-u', '--user', default="ubuntu",
                    help="User to run commands if -e option is used.\
                          Ubuntu user is used by default")

    arg = parser.parse_args()

    # Default filter if no options are specified
    filter=[]

    if arg.name:
        filter.append({'Name': 'tag-value', 'Values': ["*" + arg.name + "*"]})

    if arg.type:
        filter.append({'Name': 'instance-type', 'Values': ["*" + arg.type + "*"]})

    if arg.status:
        filter.append({'Name': 'instance-state-name', 'Values': ["*" + arg.status + "*"]})

    hosts=list_instances(filter)
    names = ""

    if arg.execute:
       for item in hosts:
          names = names + " " + item["hostname"] + "(" + item["id"] + ")"
       print ("\nCommand to execute: %s") % arg.execute
       print ("Executed by: %s") % arg.user
       print ("Hosts list: %s\n") % names 
       for item in hosts:
          if item["status"] == 'running':
             print ("::: %s (%s)") % (item["hostname"], item["id"])
             stdout,stderr = execute_cmd(item["ip"], arg.user, arg.execute)
             print (stdout) 
             print (stderr)
          else:
             print ("::: %s (%s) is not running (command execution skiped)") % (item["hostname"], item["id"])

if __name__ == '__main__':
sys.exit(main())

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为你的问题只是你不能这样打印:

print (columns_format) ("num", "Name", "Public IP", "Private IP", "ID", 
"Type", "VPC", "Status")

因为print(arguments)(arguments)试图调用函数

尝试将其替换为(我猜失去了%)

print ((columns_format) % ("num", "Name", "Public IP", "Private IP", "ID", 
"Type", "VPC", "Status"))