I'm trying to find the linenumbers of a webdocument i load using urllib2. I'm trying to do this like this:

document = urllib2.urlopen(url,'r')
page = document.read()

for index, line in enumerate(page):
	print index

This however, prints out the index of every 'character' instead of every 'line'.
How could i print out the index of the line?

You must add .split(eol) or do .partition(eol) by some end of line tag or '\n' according to your need.

In your case page is a string. To make it a list of lines modify your code this way:

document = urllib2.urlopen(url,'r')
page_lines = document.readlines()

for index, line in enumerate(page_lines):
	print index

In your case page is a string. To make it a list of lines modify your code this way:

document = urllib2.urlopen(url,'r')
page_lines = document.readlines()

for index, line in enumerate(page_lines):
	print index

This was what i was looking for , thanks a lot. It works now.

This question has already been answered. Start a new discussion instead.