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


Groups > comp.lang.python > #107611

Re: Writing different sections into a file

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From justin walters <walters.justin01@gmail.com>
Newsgroups comp.lang.python
Subject Re: Writing different sections into a file
Date Mon, 25 Apr 2016 08:00:05 -0700
Lines 105
Message-ID <mailman.83.1461596412.32212.python-list@python.org> (permalink)
References <ad6f9260-0c01-4246-9be5-84b1d2470255@googlegroups.com> <571DEBAB.6010503@gmail.com> <CAO1D73FVZnDUOfjhoEM1ddmBJ-Ls8hyBRj+AyLYqbM8b9yT2OQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de gL/KqyYSZRkfvs0nWvXtlwoMyx2vbPHR75d3BrTkDzxg==
Return-Path <walters.justin01@gmail.com>
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; 'json': 0.05; 'subject:file': 0.07; 'content:': 0.09; 'stringio': 0.09; 'subject:Writing': 0.09; 'subject:into': 0.09; 'sections': 0.13; '&gt;&gt;&gt;': 0.15; 'section.': 0.15; "skip:' 30": 0.15; "'r').read()": 0.16; '2016': 0.16; 'cstringio': 0.16; 'header:': 0.16; 'helps.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sections:': 0.16; 'stringio()': 0.16; 'wrote:': 0.16; 'email addr:gmail.com&gt;': 0.18; '>>>': 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; 'message- id:@mail.gmail.com': 0.27; 'too.': 0.30; 'url:mailman': 0.30; 'point': 0.33; 'url:python': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'this?': 0.34; 'list': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'text': 0.35; 'instance': 0.35; 'something': 0.35; 'skip:i 20': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'skip:& 10': 0.37; 'say': 0.37; 'thanks': 0.37; 'wanted': 0.37; 'received:209': 0.38; 'skip:p 20': 0.38; 'hi,': 0.38; 'format': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'hope': 0.61; 'body': 0.61; 'different': 0.63; 'great': 0.63; 'skip:\xc2 10': 0.67; '8bit%:100': 0.70; 'body:': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=XhyNJ1bfSVeLYOLbmru/VMRZYP53Dl+xoOF2th7c73s=; b=fy6p68p9PesInvzZcoUfP1lbZV8+fLtx9+D7YQJLZBDQOKPEVgqn/dIjP6+jI9ysr7 /iJfSIHzMm21YWycGDq3XNXD4Av/lDQQsXj26N1g+MQLu4msyuc2ZoS+hiAoywE26Nhn HgztYYM07qeb2dN25cq1dUMX6kr0maaKZH1wBmbgNyIXb9HRF4bKgET9+JHDEdKLZNEN 5nasMWLro+Q//TlyYIpIWBdny3cq1Uxu+v6AhovP7BDVcYMGNQTpHjsyZfTiRfrQUcMt ArybWrWFW/UXK159XBeMNWWPF6pL3QaeUXhMepc3NwaItPU+1momEkJGgXRJt4VQkW4Y tdXQ==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=XhyNJ1bfSVeLYOLbmru/VMRZYP53Dl+xoOF2th7c73s=; b=RZLCQJBCgPDmtAmZEuyygXruCkFCXBSKujknd29e/HPPY5ArcTyObHVKqghClJu+ml tgv9NGo9EZgVGfTPWC+W/lvUKUJSCFHt5puS7nnRRI91G75u6iPhhcGNYavgIojV1k1w OUDnO6PWzEoUnsf0fvN8Lj+VpxMPHRlbz23wM9qAIf508NYIZgz5MkbOrjdGyIXDUBn7 S4juN/aFlbS89F3RiuqhFKwRD8dMS9iqm8DJMqQjwQMok3GnnJ2VjrhwoQ8TdsaIFDpF F+lSBAyltxu5Q1iOAeVy2/hojCDBcYvA5ONexmRJwbadcPKZnYuC098/rF47MxFNZtb0 +4Vw==
X-Gm-Message-State AOPr4FUx5zh792h8BkVo1hl0GaUEkWRRKGYpWm6sBrG/yfZX66w6UbQufAePpNKOShWd6SpErYmGFgU/C34QDg==
X-Received by 10.112.200.41 with SMTP id jp9mr14067514lbc.65.1461596405759; Mon, 25 Apr 2016 08:00:05 -0700 (PDT)
In-Reply-To <571DEBAB.6010503@gmail.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.22
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <CAO1D73FVZnDUOfjhoEM1ddmBJ-Ls8hyBRj+AyLYqbM8b9yT2OQ@mail.gmail.com>
X-Mailman-Original-References <ad6f9260-0c01-4246-9be5-84b1d2470255@googlegroups.com> <571DEBAB.6010503@gmail.com>
Xref csiph.com comp.lang.python:107611

Show key headers only | View raw


On Mon, Apr 25, 2016 at 3:04 AM, Karim <kliateni@gmail.com> 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:

<section name="Header">
    <content>Lorem ipsum...</content>
</section>
<section name="Body">
    <content>Lorem ipsum...</content>
</section>
<section name="footer">
    <content>Lorem ipsum...</content>
</section>

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.

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


Thread

Writing different sections into a file Palpandi <palpandi111@gmail.com> - 2016-04-25 00:30 -0700
  Re: Writing different sections into a file Peter Otten <__peter__@web.de> - 2016-04-25 10:48 +0200
  Re: Writing different sections into a file harirammanohar@gmail.com - 2016-04-25 02:50 -0700
  Re: Writing different sections into a file Karim <kliateni@gmail.com> - 2016-04-25 12:04 +0200
  Re: Writing different sections into a file Grant Edwards <grant.b.edwards@gmail.com> - 2016-04-25 14:44 +0000
  Re: Writing different sections into a file justin walters <walters.justin01@gmail.com> - 2016-04-25 08:00 -0700
  Re: Writing different sections into a file Karim <kliateni@gmail.com> - 2016-04-25 17:12 +0200

csiph-web