CarpeNoctem
CarpeNoctem

Reputation: 5660

Python: write multiple str.format() lines to a file?

for host in platforms:
    f = open(host, 'w')
    f.write('define host {\n')
    f.write('    host_name            {}\n'.format(host))
    f.write('    alias                {}\n'.format(host))
    f.write('    display_name         {}\n'.format(host))
    f.write('    address              {}\n'.format(str(socket.gethostbyname(host))))
    f.write('    use                  linux-server\n')
    f.write('    register             1\n')
    f.write('}\n')

Is there a better way? Is there a simple way to format all these string but only do one write method call? If the above is considered best practice that's fine, just seems like it could be prettier some other way.

Upvotes: 0

Views: 1522

Answers (3)

eyquem
eyquem

Reputation: 27575

You ended up reverting back... Would you prefer one of these possibilities ? :

def print_host(host,serv,numb, ch = ('define host {{\n'
                                     '    {0:<21}{6}\n'
                                     '    {1:<21}{6}\n'
                                     '    {2:<21}{6}\n'
                                     '    {3:<21}{7}\n'
                                     '    {4:<21}{8}\n'
                                     '    {5:<21}{9}\n'
                                     '}}\n'            ) ):

    f = open(host, 'w')
    f.write(ch.format('host_name','alias','display_name',
                      'address','use','register',
                      host,
                      str(socket.gethostbyname(host)),serv,numb))

or

def print_host(host,serv,numb):

    tu = ('define host {',
          '    {:<21}{}'.format('host_name',host),
          '    {:<21}{}'.format('alias',host),
          '    {:<21}{}'.format('display_name',host),
          '    {:<21}{}'.format('address',str(socket.gethostbyname(host))),
          '    {:<21}{}'.format('use',serv),
          '    {:<21}{}'.format('register',numb),
          '}\n')

    f = open(host, 'w')
    f.write('\n'.join(tu))

or

def print_host(host,serv,numb):
    f = open(host, 'w')
    f.writelines(('define host {\n',
                  '    {:<21}{}\n'.format('host_name',host),
                  '    {:<21}{}\n'.format('alias',host),
                  '    {:<21}{}\n'.format('display_name',host),
                  '    {:<21}{}\n'.format('address',str(socket.gethostbyname(host))),
                  '    {:<21}{}\n'.format('use',serv),
                  '    {:<21}{}\n'.format('register',numb),
                  '}\n'))

or ( I prefer this neater one)

def print_host(host,serv,numb):
    f = open(host, 'w')
    f.writelines(('define host {\n',
                  '    host_name            {}\n'.format(host),
                  '    alias                {}\n'.format(host),
                  '    display_name         {}\n'.format(host),
                  '    address              {}\n'.format(str(socket.gethostbyname(host))),
                  '    use                  {}\n'.format(serv),
                  '    register             {}\n'.format(numb),
                  '}\n'))

Upvotes: 0

Keith
Keith

Reputation: 43024

You can use named substitutions in a long, triple-quoted string.

def print_host(host, address):
    f = open(host, 'w')
    f.write("""define host {{
        host_name            {host}
        alias                {host}
        display_name         {host}
        address              {address}
        use                  linux-server
        register             1
    }}\n""".format(host=host, address=address))

print_host("myhost", "10.10.10.10")

But note that you must double your curly braces to escape them here.

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798646

Triple-quoted strings.

f.write('''{} {}
{}'''.format(1, 2, 3))

Upvotes: 0

Related Questions