Reading a portion of a file

cmfvulcanius at gmail.com cmfvulcanius at gmail.com
Thu Mar 8 13:35:44 EST 2007


On Mar 8, 12:50 pm, "Jordan" <jordan.tayl... at gmail.com> wrote:
> On Mar 8, 12:46 pm, "Jordan" <jordan.tayl... at gmail.com> wrote:
>
>
>
> > On Mar 8, 11:52 am, "Rune Strand" <rune.str... at gmail.com> wrote:
>
> > > On Mar 8, 5:12 pm, cmfvulcan... at gmail.com wrote:
>
> > > > I am using a script with a single file containing all data in multiple
> > > > sections. Each section begins with "#VS:CMD:command:START" and ends
> > > > with "#VS:CMD:command:STOP". There is a blank line in between each
> > > > section. I'm looking for the best way to grab one section at a time.
> > > > Will I have to read the entire file to a string and parse it further
> > > > or is it possible to grab the section directly when doing a read? I'm
> > > > guessing regex is the best possible way. Any help is greatly
> > > > appreciated.
>
> > > Seems like something along these line will do:
>
> > > _file_ = "filepart.txt"
>
> > > begin_tag = '#VS:CMD:command:START'
> > > end_tag = '#VS:CMD:command:STOP'
>
> > > sections = []
> > > new_section = []
> > > for line in open(_file_):
> > >     line = line.strip()
> > >     if begin_tag in line:
> > >         new_section = []
> > >     elif end_tag in line:
> > >         sections.append(new_section)
> > >     else:
> > >         if line: new_section.append(line)
>
> > > for s in sections: print s
>
> > > If your want more control, perhaps flagging "inside_section",
> > > "outside_section" is an idea.
>
> > You probably don't want to use regex for something this simple; it's
> > likely to make things even more complicated.  Is there a space between
> > the begin_tag and the first word of a section (same question with the
> > end_tag)?
>
> Sent the post too soon.  What is the endline character for the file
> type?  What type of file is it?    An example section would be nice
> too.  Cheers.

Ok, regex was my first thought because I used to use grep with Perl
and shell scripting to grab everything from one pattern to another
pattern. The file is just an unformatted file. What is below is
exactly what is in the file. There are no spaces between the beginning
and ending tags and the content. Would you recommend using spaces
there? And if so, why?

A sample of the file:

#VS:COMMAND:df:START
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/vzfs             20971520    517652  20453868   3% /
tmpfs                  2016032        44   2015988   1% /var/run
tmpfs                  2016032         0   2016032   0% /var/lock
tmpfs                  2016032         0   2016032   0% /dev/shm
tmpfs                  2016032        44   2015988   1% /var/run
tmpfs                  2016032         0   2016032   0% /var/lock
#VS:COMMAND:df:STOP

#VS:FILE:/proc/loadavg:START
0.00 0.00 0.00 1/32 14543
#VS:FILE:/proc/loadavg:STOP

#VS:FILE:/proc/meminfo:START
MemTotal:       524288 kB
MemFree:        450448 kB
Buffers:             0 kB
Cached:              0 kB
SwapCached:          0 kB
Active:              0 kB
Inactive:            0 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       524288 kB
LowFree:        450448 kB
SwapTotal:           0 kB
SwapFree:            0 kB
Dirty:               0 kB
Writeback:           0 kB
Mapped:          73840 kB
Slab:                0 kB
CommitLimit:         0 kB
Committed_AS:   248704 kB
PageTables:          0 kB
VmallocTotal:        0 kB
VmallocUsed:         0 kB
VmallocChunk:        0 kB
#VS:FILE:/proc/meminfo:STOP

#VS:FILE:/proc/stat:START
cpu  67188 0 26366 391669264 656686 0 0
cpu0 24700 0 10830 195807826 373309 0 0
cpu1 42488 0 15536 195861438 283376 0 0
intr 0
swap 0 0
ctxt 18105366807
btime 1171391058
processes 26501285
procs_running 1
procs_blocked 0
#VS:FILE:/proc/stat:STOP

#VS:FILE:/proc/uptime:START
1962358.88 1577059.05
#VS:FILE:/proc/uptime:STOP




More information about the Python-list mailing list