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


Groups > comp.lang.python > #107585 > unrolled thread

Writing different sections into a file

Started byPalpandi <palpandi111@gmail.com>
First post2016-04-25 00:30 -0700
Last post2016-04-25 17:12 +0200
Articles 7 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#107585 — Writing different sections into a file

FromPalpandi <palpandi111@gmail.com>
Date2016-04-25 00:30 -0700
SubjectWriting different sections into a file
Message-ID<ad6f9260-0c01-4246-9be5-84b1d2470255@googlegroups.com>
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

[toc] | [next] | [standalone]


#107588

FromPeter Otten <__peter__@web.de>
Date2016-04-25 10:48 +0200
Message-ID<mailman.67.1461574128.32212.python-list@python.org>
In reply to#107585
Palpandi wrote:

> 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?

If the data easily fits into memory just keep it in one list per section

header = [...]
body = [...]
footer = [...]

...
body.append("something more\n")
footer[:] = ["new footer\n"]
...

with open(filename, "w") as f:
    for section in (header, body, footer):
        f.writelines(section)


If the sections are not known in advance put them into a dictionary or a 
list.

If you are free to choose the file format you may use
https://docs.python.org/dev/library/configparser.html

[toc] | [prev] | [next] | [standalone]


#107590

Fromharirammanohar@gmail.com
Date2016-04-25 02:50 -0700
Message-ID<3c1cfc62-fb1e-4538-8c8d-0a9525297f91@googlegroups.com>
In reply to#107585
On Monday, April 25, 2016 at 1:01:12 PM UTC+5: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 ConfigParser..

[toc] | [prev] | [next] | [standalone]


#107592

FromKarim <kliateni@gmail.com>
Date2016-04-25 12:04 +0200
Message-ID<mailman.69.1461578670.32212.python-list@python.org>
In reply to#107585

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

[toc] | [prev] | [next] | [standalone]


#107608

FromGrant Edwards <grant.b.edwards@gmail.com>
Date2016-04-25 14:44 +0000
Message-ID<mailman.81.1461595506.32212.python-list@python.org>
In reply to#107585
On 2016-04-25, Palpandi <palpandi111@gmail.com> 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 makes you think a temporary file isn't best?

> What is the better datatype to achieve this?

When in doubt, try the simplest approach first:

Use a string (or byte-string if it's not text) for each of the header,
body, and footer.  Append data to each as desired and then write them
out to a file when you're done.

If that's not workable, explain why, and we can tell you what to try
next (probably a stringio for each section, or a list of strings or
byte-strings for each section, or temporary files).

-- 
Grant Edwards               grant.b.edwards        Yow! Wait ... is this a FUN
                                  at               THING or the END of LIFE in
                              gmail.com            Petticoat Junction??

[toc] | [prev] | [next] | [standalone]


#107611

Fromjustin walters <walters.justin01@gmail.com>
Date2016-04-25 08:00 -0700
Message-ID<mailman.83.1461596412.32212.python-list@python.org>
In reply to#107585
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.

[toc] | [prev] | [next] | [standalone]


#107612

FromKarim <kliateni@gmail.com>
Date2016-04-25 17:12 +0200
Message-ID<mailman.84.1461597183.32212.python-list@python.org>
In reply to#107585

On 25/04/2016 17:00, justin walters wrote:
> 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.

Great ideas!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web