Text parsing

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Aug 20 09:00:42 EDT 2006


In <mailman.9569.1156073770.27775.python-list at python.org>, Michiel Sikma
wrote:

> 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 ''.join(block).strip()

Because your line "test10\n" is still in `block` at this point.

> 	yield '\n'
> 
> […]
>
> ---- 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.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list