tags:

views:

67

answers:

3

I open a website with urlopen. I then put the website sourcecode into a variable like so

source = website.read()

When I just print the source it comes out formatted correctly, however when I try to iterate through each line each character is it's own line.

for example

when I just print it looks like this

<HTML> title</html>

When I do this

for line in source:
      print line

it looks like this

<
H
T
M
L
... etc

I need to find a string that starts with "var" and then print that entire line.

+4  A: 

Use readlines() instead of read() to get a list of lines.

miles82
Ok I found my data. The problem is the data is the total string is thisvar myData = [["NAME","NUM","CLASS", "ADDY", "ID"],[...]];How can I separate the groups?
j00niner
Please edit your question and show an example of what you want.
miles82
A: 

Maybe it is better if you parse it with regex or better and HTML parser.

Anyway you can do:

import urllib2

for item in urllib2.urlopen("http://www.python.org"):
    print '->', item

and you get the different lines:

-> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. or /TR/xhtml1/DTD/xhtml1-transitional.dtd">
->
->
-> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-> 
-> <head>
->   <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
->   <title>Python Programming Language -- Official Website</title> 
->   <meta name="keywords" content="python programming language object oriented web  free source" />
->   <meta name="description" content="      Home page for Python, an interpreted, interactive,  object-oriented, extensible

.........etc
joaquin
+1  A: 

Or use:

for line in source.split("\n"):
    ...
wump