Create Sections In Python
I am newbie to Python. I have large file with repetitive string through the logs Example: abc def efg gjk abc def efg gjk abc def efg gjk abc def efg gjk Expected Result ---------
Solution 1:
If a section is defined by the starting line, you can use a generator function to yield sections from an input iterable:
def per_section(iterable):
section = []
for line in iterable:
if line.strip() == 'abc':
# start of a section, yield previousif section:
yield section
section = []
section.append(line)
# lines done, yield lastif section:
yield section
Use this with an input file, for example:
withopen('somefile') as inputfile:
for i, section inenumerate(per_section(inputfile)):
print'------- section {} ---------'.format(i)
print''.join(section)
If sections are simply based on the number of lines, use the itertools
grouper recipe to group the input iterable into groups of a fixed length:
from itertools import izip_longest
defgrouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
withopen('somefile') as inputfile:
for i, section inenumerate(grouper(inputfile, 4, '\n')):
print'------- section {} ---------'.format(i)
print''.join(section)
Solution 2:
To make it simple (as you say: one record every 4 lines):
with open ('yourfile', 'r') as f: lines = [x for x in f]
whilelines:
print ('----------------------------------')
print (lines [:4] )
lines = lines [4:]
Solution 3:
Since you have a known starting point, you can trigger section headers whenever you see the section start:
>>> section = 0>>> withopen('bigdata.txt') as f:
for line in f:
if'abc'in line:
section += 1print ('Section' + str(section)).center(55, '-')
print line
------------------------Section1-----------------------
abc
defefg
gjk
------------------------Section2-----------------------
abc
defefg
gjk
------------------------Section3-----------------------
abc
defefg
gjk
------------------------Section4-----------------------
abc
defefg
gjk
Post a Comment for "Create Sections In Python"