Reading a portion of a file
attn.steven.kuo at gmail.com
attn.steven.kuo at gmail.com
Thu Mar 8 21:38:14 EST 2007
On Mar 8, 10:35 am, cmfvulcan... at gmail.com wrote:
(snipped)
>
> 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:
You can use iterators:
import StringIO
import itertools
def group(line):
if line[-6:-1] == 'START':
group.current = group.current + 1
return group.current
group.current = 0
data = """
#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
""".lstrip("\n");
fh = StringIO.StringIO(data)
sections = itertools.groupby(itertools.ifilter(lambda line: len(line)
> 1, fh),
lambda line: group(line))
for key, section in sections:
for line in section:
print key, line,
--
Hope this helps,
Steven
More information about the Python-list
mailing list