tags:

views:

67

answers:

3

I want to read file including spaces in each lines

My current code

def data():

f = open("save.aln")

for line in f.readlines():

    print "</br>"

    print line

I am using python and output embedded in html

File to be read - http://pastebin.com/EaeKsyvg

Thanks

+2  A: 

It seems that your problem is that you need space preserving in HTML. The simple solution would be to put your output between <pre> elemenets

def data(): 
    print "<pre>"
    f = open("save.aln") 
    for line in f.readlines(): 
        print line
    print "</pre>"

Note that in this case you don't need the <br> elements either, since the newline characters are also preserved.

itsadok
:‎‎‎‎‎‎‎‎‎‎‎‎‎‎
Ofri Raviv
Thanks itsadok :)
+1  A: 

The problem that you are faced with is that HTML ignores multiple whitespaces. @itsadok's solution is great. I upvoted it. But, it's not the only way to do this either.

If you want to explicitly turn those whitespaces into HTML whitespace characters, you could to this:

def data():
    f = open("save.aln")
    for line in f.readlines():
        print "<br />"
        print line.replace(" ", "&nbsp")

Cheers

inspectorG4dget
A: 
import cgi
with open('save.aln') as f:
    for line in f:
        print cgi.escape(line) # escape <, >, &
        print '<br/>'
Olivier