Text parsing

M

Michiel Sikma

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
(e-mail address removed)
 
M

Marc 'BlackJack' Rintsch

Michiel Sikma said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top