Reputation: 3282
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
Upvotes: 0
Views: 188
Reputation: 49216
import cgi
with open('save.aln') as f:
for line in f:
print cgi.escape(line) # escape <, >, &
print '<br/>'
Upvotes: 0
Reputation: 114035
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(" ", " ")
Cheers
Upvotes: 1
Reputation: 29342
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.
Upvotes: 2