Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Rob Hills Newsgroups: comp.lang.python Subject: Re: Generate config file from template using Python search and replace. Date: Mon, 30 Nov 2015 10:40:29 +0800 Lines: 30 Message-ID: References: <73046000-f634-40f2-9c83-f03a5db134e6@googlegroups.com> <4f923003-4f85-4a69-bfda-165194211bb4@googlegroups.com> <11d4423f-7912-426c-9d20-d2cb5efba18b@googlegroups.com> Reply-To: rhills@medimorphosis.com.au Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de U/XK0VwUa08V0QPHodETHATxqsFzXxtZeUsWBCY0wTFQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'below)': 0.07; 'subject:file': 0.07; '"r")': 0.09; 'os.path': 0.09; 'script,': 0.09; 'subject:using': 0.09; 'python': 0.10; 'template': 0.11; 'subject: \n ': 0.15; 'magic': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:Generate': 0.16; 'subject:search': 0.16; 'wrote:': 0.16; 'string': 0.17; 'exists': 0.18; '(see': 0.20; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; 'heading': 0.27; 'print': 0.30; 'values.': 0.33; 'open': 0.33; 'skip:d 20': 0.34; 'but': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'seem': 0.37; 'does': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'challenge': 0.61; 'australia': 0.61; 'charset:windows-1252': 0.62; 'header:Reply-To:1': 0.67; 'reply- to:no real name:2**0': 0.71; 'me!': 0.84; 'western': 0.89; 'hills': 0.93 X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <11d4423f-7912-426c-9d20-d2cb5efba18b@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99715 A program I am writing at present does exactly this and I simply do multiple calls to string.replace (see below) On 30/11/15 10:31, Mr Zaug wrote: > I seem to be heading in this direction. > > #!/usr/bin/env python > import re > from os.path import exists > > script, template_file =3D argv > print "Opening the template file..." > > with open (template_file, "r") as a_string: > data=3Da_string.read().replace('BRAND', 'Fluxotine') data=3Ddata.replace('STRING_2', 'New String 2') data=3Ddata.replace('STRING_3', 'New String 3') > print(data) > > So now the challenge is to use the read().replace magic for multiple va= lues. It's crude, but it works well for me! --=20 Rob Hills Waikiki, Western Australia