0

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?

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Aeronobe
0

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

0

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
0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.