Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99803

Re: Generate config file from template using Python search and replace.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Generate config file from template using Python search and replace.
Date Tue, 01 Dec 2015 16:43:33 +0100
Organization None
Lines 25
Message-ID <mailman.76.1448984635.14615.python-list@python.org> (permalink)
References <73046000-f634-40f2-9c83-f03a5db134e6@googlegroups.com> <mailman.3.1448752087.14615.python-list@python.org> <4f923003-4f85-4a69-bfda-165194211bb4@googlegroups.com> <mailman.11.1448837430.14615.python-list@python.org> <0dd0db34-7f8f-4e8b-ad6a-c82ad19c2e5e@googlegroups.com> <mailman.33.1448874871.14615.python-list@python.org> <8c787187-910c-40df-9235-9e4c2dafb19c@googlegroups.com> <mailman.64.1448966994.14615.python-list@python.org> <076b883f-f649-48e0-a064-f05d4d6c5909@googlegroups.com> <mailman.75.1448980899.14615.python-list@python.org> <b0a59a2b-5753-435c-95bd-fcff6b655999@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de x3AukdgnwCQspKXQ0UVp2wpoMcYAfvZvNDPmwWBzBJ+w==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'method.': 0.05; 'filename': 0.07; 'formatting': 0.07; 'subject:file': 0.07; '"w")': 0.09; 'filename,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'naming': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Generate': 0.16; 'subject:search': 0.16; 'wrote:': 0.16; 'string': 0.17; 'skip:" 40': 0.20; 'variables.': 0.22; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'values': 0.28; 'name?': 0.29; 'convert': 0.29; "can't": 0.32; 'open': 0.33; 'file': 0.34; 'text': 0.35; 'expected': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'stuff': 0.38; 'skip:o 20': 0.38; 'whatever': 0.39; 'subject:from': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'serial': 0.70; 'brand': 0.75; 'too:': 0.84; 'site?': 0.91; 'write()': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9ec4.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99803

Show key headers only | View raw


Mr Zaug wrote:

> That makes sense.
> 
> So I still can't see how to write the string object to a file whist naming
> the file with whatever values I provided for the NNN and BRAND variables.
> 
> Printing the contents of the string object is working with all the
> expected substitutions. Do I need to convert the string object into a file
> before I write it? Or do I need to open a new file and somehow stuff the
> string object into it?

Yes, open a new file and write the string using the write() method. You can 
use string formatting to build the filename, too:

text = ... # the string you want to write 

nnn = raw_input("What is the serial number of the site? ")
brand = raw_input("What is the brand, or product name? ")

filename = "{NNN}{BRAND}_farm.any".format(BRAND=brand, NNN=nnn)
with open(filename, "w") as outstream:
    outstream.write(text)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-28 13:45 -0800
  Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-28 14:04 -0800
  Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-29 00:07 +0100
    Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 12:23 -0800
      Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-29 23:50 +0100
        Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 17:28 -0800
          Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-30 10:14 +0100
            Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-30 20:54 -0800
              Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 11:49 +0100
                Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 05:18 -0800
                Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 06:18 -0800
                Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 15:41 +0100
                Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 07:21 -0800
                Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 16:43 +0100
                Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 10:43 -0800
        Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 18:31 -0800
          Re: Generate config file from template using Python search and replace. Rob Hills <rhills@medimorphosis.com.au> - 2015-11-30 10:40 +0800
          Re: Generate config file from template using Python search and replace. MRAB <python@mrabarnett.plus.com> - 2015-11-30 03:23 +0000

csiph-web