Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Karim Newsgroups: comp.lang.python Subject: Re: Writing different sections into a file Date: Mon, 25 Apr 2016 17:12:59 +0200 Lines: 107 Message-ID: References: <571DEBAB.6010503@gmail.com> <571E33FB.3080200@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Yb+9QeQEKPgtMDpsK/nhlQiT/ctpo/+X8LTlbaNVI5eA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'json': 0.05; 'subject:file': 0.07; 'content:': 0.09; 'stringio': 0.09; 'subject:Writing': 0.09; 'subject:into': 0.09; 'sections': 0.13; 'section.': 0.15; "skip:' 30": 0.15; "'r').read()": 0.16; '2016': 0.16; 'cstringio': 0.16; 'header:': 0.16; 'helps.': 0.16; 'justin': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sections:': 0.16; 'stringio()': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'library': 0.20; 'object.': 0.22; 'parse': 0.22; 'file.': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; 'header': 0.24; 'mon,': 0.24; 'header:User-Agent:1': 0.26; 'too.': 0.30; 'url:mailman': 0.30; 'point': 0.33; 'url:python': 0.33; 'message-id:@gmail.com': 0.34; 'url:listinfo': 0.34; 'file': 0.34; 'this?': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'text': 0.35; 'instance': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'thanks': 0.37; 'wanted': 0.37; 'skip:p 20': 0.38; 'hi,': 0.38; 'format': 0.39; 'received:192': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'hope': 0.61; 'body': 0.61; 'charset:windows-1252': 0.62; 'different': 0.63; 'great': 0.63; 'body:': 0.84; 'walters': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=35Efkmv/E5jksVOT0JSJm/TZ6dMaFxWw7DMXLUGrXpQ=; b=oyt3/J4flZJBqs2Qa2sgRRHD161/eoy0P11p2pqUDeh3MpTqy9ZSTvWgnd4eCeNdpU Ltcd7rqx/lb//O8SuyNuhVRqvQv1W9f149wcLnUZEweISh50HM7XnRc1njiRPXILxyYA wREdpeGFqaF8uge5pe1nTJMlMLohAdt9rXOKvpoag9+3dBBhS+KfWjNNcQKiyFShL9j0 06cxkk5xwWYIsd4wAJABdNlxIjI172DeCFxxLR51rtzmGoC48c+Tq5T+biugnzPEycer 3u0Hi2SN6k6pgStEJykRWQujWooxyPYqnnXrwimK+dZotrDZk92xEp+WTZW3uHPueTB7 Ie6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=35Efkmv/E5jksVOT0JSJm/TZ6dMaFxWw7DMXLUGrXpQ=; b=keNI9kuWjjEhYELb06gWf3AIxS/jzl0HcTXbcDByWWdahNuQJp3vyhvsRtqy/QQVai nW8/XdK0I+zC0W5KHFWZE5gFrTsTTUmJNwWy79jVxsD9IjzfZm6dVwg2fQrZwsh2j9f2 7KzdabjG356ICzWaz+cnwtwEIfQZnBJjjpHCvVQINTAptuMefTODeepvydwM94IN6fPr LweRAA40h1HjydLumsCPsYprrdX2vr7yTgbzFZQFw43AEaZMst7VrFA0bKgDrmP2RsJK kd6OYA9jWLLLmyYZKmo9lQH3p4waWNVm4yhqOkUPmzxvv9Ho3nyyySIhQOvPuhGnrcUi Cb/g== X-Gm-Message-State: AOPr4FVPE3hsvHVV51iq9D8j7Zeajl86J166KVZnYDZsK275G4IMh1z7POs9ronuu//sKw== X-Received: by 10.28.113.218 with SMTP id d87mr13675038wmi.52.1461597181694; Mon, 25 Apr 2016 08:13:01 -0700 (PDT) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <571E33FB.3080200@gmail.com> X-Mailman-Original-References: <571DEBAB.6010503@gmail.com> Xref: csiph.com comp.lang.python:107612 On 25/04/2016 17:00, justin walters wrote: > On Mon, Apr 25, 2016 at 3:04 AM, Karim wrote: > >> >> On 25/04/2016 09:30, Palpandi wrote: >> >>> Hi, >>> >>> I need to write different sections into a file. >>> At any point of time, content can be added to any section. >>> >>> I don't want keep each section into a temporary file. >>> What is the better way to store the contents of each section and write >>> them into a file at the end? >>> What is the better datatype to achieve this? >>> >>> >>> Thanks and Regards, >>> Palpandi >>> >> Use Stringio: >> - >> >> from cStringIO import StringIO >> >> content = StringIO() >> >> # Header >> content.write('; Header\n') >> content.write('; Body'\n) >> content.write('; Footer\n') >> >> open('my_file', 'wb').write(content.getvalue()) >> >> - >> Karim >> >> >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > > All of the other answers are great too. I was thinking that you could > format the text file to have dividers for each section. For instance it may > look something like this: > > Header > Lorem ipsum.... > <---> > Body > Lorem ipsum... > <---> > Footer > Lorem ipsum.. > > Then, you could create a list of sections like so: > > file = open('file.txt', 'r').read() > > section_list = file.split('<--->') > > print(section_list[0]) > > Header > Lorem ipsum... > > Say you wanted to get really fancy, you could even format the file to have > named sections like so: > >
> Lorem ipsum... >
>
> Lorem ipsum... >
>
> Lorem ipsum... >
> > Then you can use the xmlparser library to parse the file into an xml object. > > > Alternatively, you could also use JSON formatting: > > { > sections: { > header: { > title: "header", > content: "Lorem ipsum..." > }, > body: { > title: "Body", > content: "Lorem ipsum..." > }, > footer: { > title: "Footer", > content: "Lorem ipsum..." > } > } > } > > I hope this helps. Great ideas!