0

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

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by masterofpuppets
0
lines = text.split("\n")
for i, l in enumerate(lines):
    print "line %d: %s" %(i + 1, l)
1

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

Edited by Paul Thompson: n/a

Votes + Comments
congrats
0

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 :)

Edited by masterofpuppets: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.