Text parsing

Michiel Sikma michiel at thingmajig.org
Sun Aug 20 07:24:50 EDT 2006


Hello everybody.

Inspired by an example from the book Beginning Python: From Novice to  
Professional, I started working on a simple text parser which I can  
hopefully then extend into a more comprehensive system. I've got a  
little problem, though.

My code:

---- test.py ----
import sys
	
def preparse(file):
	block = []
	for line in file:
		if line.strip():
			block.append(line)
		elif block:
			yield ''.join(block).strip()
			block = []
	yield '\n'

def makeList(file):
	testOutput = list(preparse(file))
	print testOutput

testInput = open("test", "r")

makeList(testInput)
----

---- test ----
test1
test2

test3
test4
test5
test6

test7
test8

test9

test10
----

When I run test.py, it prints this:
michiel-sikmas-computer:~/Desktop msikma$ python test.py
['test1\ntest2', 'test3\ntest4\ntest5\ntest6', 'test7\ntest8',  
'test9', '\n']

What happened to "test10"? It seems to be gone unless I add two  
linebreaks at the end of the file.

Greets,

Michiel Sikma
michiel at thingmajig.org





More information about the Python-list mailing list