Download and decompress gzipped file in memory?

You need to seek to the beginning of compressedFile after writing to it but before passing it to gzip.GzipFile(). Otherwise it will be read from the end by gzip module and will appear as an empty file to it. See below: #! /usr/bin/env python import urllib2 import StringIO import gzip baseURL = “https://www.kernel.org/pub/linux/docs/man-pages/” filename = … Read more

Convert io.BytesIO to io.StringIO to parse HTML page

the code in the accepted answer actually reads from the stream completely for decoding. Below is the right way, converting one stream to another, where the data can be read chunk by chunk. # Initialize a read buffer input = io.BytesIO( b’Inital value for read buffer with unicode characters ‘ + ‘ÁÇÊ’.encode(‘utf-8’) ) wrapper = … Read more

Do I have to do StringIO.close()?

Generally it’s still better to call close() or use the with statement, because there may be some unexpected behaviour in special circumstances. For example, the expat-IncrementalParser seems to expect a file to be closed, or it won’t return the last tidbit of parsed xml until a timeout occurs in some rare circumstances. But for the … Read more

Python Flask send_file StringIO blank files

The issue here is that in Python 3 you need to use StringIO with csv.write and send_file requires BytesIO, so you have to do both. @app.route(‘/test_download’) def test_download(): row = [‘hello’, ‘world’] proxy = io.StringIO() writer = csv.writer(proxy) writer.writerow(row) # Creating the byteIO object from the StringIO Object mem = io.BytesIO() mem.write(proxy.getvalue().encode()) # seeking was … Read more

how do I clear a stringio object?

TL;DR Don’t bother clearing it, just create a new one—it’s faster. The method Python 2 Here’s how I would find such things out: >>> from StringIO import StringIO >>> dir(StringIO) [‘__doc__’, ‘__init__’, ‘__iter__’, ‘__module__’, ‘close’, ‘flush’, ‘getvalue’, ‘isatty’, ‘next’, ‘read’, ‘readline’, ‘readlines’, ‘seek’, ‘tell’, ‘truncate’, ‘write’, ‘writelines’] >>> help(StringIO.truncate) Help on method truncate in module … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)