I have a .txt file with text like the following:
Project Gutenberg Australia
a treasure-trove of literature
treasure found hidden with no evidence of ownership
but whenever I try to read this file in Python (using Flask - I'm uploading the file to a site), using the following lines:
if request.method == 'POST':
f = request.files['file']
f.save(secure_filename(f.filename))
f.stream.seek(0)
content = f.read()
return render_template("book.html", text=content)
My "book.html" file is like the following:
<pre>
{{ text }}
</pre>
I get something like the following:
b'\xef\xbb\xbf\r\nProject Gutenberg Australia\r\na treasure-trove of literature\r\ntreasure found hidden with no evidence of ownership\r\n\r\n\r\n\r\n\r\...]
How can I fix this so that what is displayed on my site is just like what is displayed in the .txt file? Do I need to modify "book.html" or just read the file differently with Python?
Thanks!
You just need to .decode()
your bytes:
print(b'\xef\xbb\xbf\r\nProject Gutenberg Australia\r\na treasure-trove of literature\r\ntreasure found hidden with no evidence of ownership\r\n\r\n\r\n\r\n\r\...]'.decode())
gives:
Project Gutenberg Australia
a treasure-trove of literature
treasure found hidden with no evidence of ownership
\...]