1

So I have a .htm file which I need to split into different lines and store in a list. So the code I used for this was as follows.

html = open('filename.htm').read().splitlines()

However when I do the following, the entire file is printed out at once and not line by line and so it seems the input from the file is not being split into different lines

for line in html:
    print(line)

Additionally, when i try to copy paste the contents of the file to the program, it works fine.

html = raw_input()
html = html.splitlines()

Could anyone tell me how to get the input from the file to be split into different lines? Thanks.

4 Answers 4

3

Since for line in html: print(line) means print all the lines, it obviously print the entire files, one line at the time.

So, unless you file is "consolidated" on a single line of text, it's working. You can add some text output to ascertain it:

html = open('try.htm').read().splitlines()
for line in html:
    print("Line:")
    print(line)
    print()
6
  • sorry, perhaps i was not being clear. The program prints all the lines. But it prints them at once and not line by line which is what I want.
    – nave
    May 19, 2013 at 7:16
  • Can you upload your input file somewhere? Maybe that's the problem. Or maybe I misunderstood what you meant by "line by line".
    – Dek Dekku
    May 19, 2013 at 7:19
  • I am not sure where to upload it...but its is from the page source of this link : osvdb.org/show/osvdb/93456
    – nave
    May 19, 2013 at 7:22
  • Yes, that is what i would like to have. Could you paste your exact code here?
    – nave
    May 19, 2013 at 7:33
  • Well...weirdly i am still getting my original output...there is no change with your code. The 'Line:' is printed out and then the entire source file at once and then finally the '()' gets printed out. Could I know what version of python your using?
    – nave
    May 19, 2013 at 7:40
2

You don't want the read() part in your first call. Try this:

htmllines = open('filename.htm').readlines()
for line in html:
    print line

If your HTML file is definitely on multiple lines not just one line as Dek suggests, then make sure you have the right encoding for your file - you might need to decode as follows:

htmllines = open('filename.htm').read().decode("Some Encoding").splitlines()
for line in html:
    print line
2
  • So i tried readlines() but its the same....everything is still printed out at once which indicates the file input is not being split into various lines...btw, what encoding do you suppose i try?
    – nave
    May 19, 2013 at 7:27
  • The most likely encoding to check is 'Windows-1252' or 'Latin-1' but to be honest I'm not sure if that's actually what the problem is. Try going into the Python debugger and running: >> f = open('filename.htm') then trying a different .decode('encoding') calls to see if you get a nicer output.
    – Patrick
    May 19, 2013 at 7:38
0

what about file.readlines([sizehint]) ?

0

You need readlines() and not splitlines(). And your code goes like below

all_lines = open('sample.html').readlines()

for line in all_lines:
    print(line)

To store as a list you can do

lst = list(open('sample.html').readlines())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.