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?

Recommended Answers

All 4 Replies

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

Except that in HTML line changes have not so much meaning.

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.

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.