0

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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
j00niner
  • 341
  • 1
  • 3
  • 3
  • type(source) is or . Iterating over either of those gives you the individual characters of the string. – msw May 06 '10 at 07:08
  • 3
    and you really, really, really, don't want to parse HTML with simple string matching, really http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#answer-1732454 – msw May 06 '10 at 07:10

2 Answers2

5

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

miles82
  • 6,584
  • 38
  • 28
  • Ok I found my data. The problem is the data is the total string is this var myData = [ ["NAME","NUM","CLASS", "ADDY", "ID"], [...] ]; How can I separate the groups? – j00niner May 06 '10 at 07:32
  • Please edit your question and show an example of what you want. – miles82 May 06 '10 at 07:52
1

Or use:

for line in source.split("\n"):
    ...
wump
  • 4,277
  • 24
  • 25