Hey guys I have a wee problem I need some help with.

How would I read in lines of text one at a time, keeping track of the line numbers, only stopping when a line is read that contains only a single full-stop.

Example:

It is a briskly blowing wind that blows
from the north, the North of my youth.
The wind is cold too, colder than the
winds of yesteryear.
.

I cant figure out a way of seperating the input into different lines e.g line1 = "It is a briskly blowing wind that blows" as I need to use each line later in the full problem.

Thanks

Recommended Answers

All 3 Replies

lines = text.split("\n")
for i, l in enumerate(lines):
    print "line %d: %s" %(i + 1, l)

For storing them you can use a dictionary:

lines = text.split("\n")
lineDic = {}
for i,l in enumerate(lines):
    lineDic["Line %i"%s] = l
print lineDic

Hope that helps :)

EDIT: woo! 900 posts :P

commented: congrats +4
Member Avatar for masterofpuppets

hi,
if you're talking about raw input from the keyboard this should do:

linesCount = 0
print "Enter text ending in a '.':"
line = raw_input()
 
while line != ".":
    linesCount += 1
    line = raw_input()
 
print "The number of lines is", linesCount

otherwise, i.e. file input, the above solutions should give you some ideas.

hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.