Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107611
| From | justin walters <walters.justin01@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Writing different sections into a file |
| Date | 2016-04-25 08:00 -0700 |
| 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> |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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