stringio

Python decompress gzip data in memory without file

左心房为你撑大大i 提交于 2019-12-02 04:31:41
问题 I have gzipped data from HTTP reply. I have following code: def gzipDecode(self, content): import StringIO import gzip outFilePath = 'test' compressedFile = StringIO.StringIO(content) decompressedFile = gzip.GzipFile(fileobj=compressedFile) with open(outFilePath, 'w') as outfile: outfile.write(decompressedFile.read()) data = '' with open(outFilePath, 'r') as myfile: data=myfile.read().replace('\n', '') return data which decompress input gzipped content and return string (http reply is gzipped

Extract zip to memory, parse contents

时间秒杀一切 提交于 2019-12-02 03:51:36
I want to read the contents of a zip file into memory rather than extracting them to disc, find a particular file in the archive, open the file and extract a line from it. Can a StringIO instance be opened and parsed? Suggestions? Thanks in advance. zfile = ZipFile('name.zip', 'r') for name in zfile.namelist(): if fnmatch.fnmatch(name, '*_readme.xml'): name = StringIO.StringIO() print name # prints StringIO instances open(name, 'r') # IO Error: No such file or directory... I found a few similar posts, but none that seem to address this issue: Extracting a zipfile to memory? IMO just using read

Python decompress gzip data in memory without file

跟風遠走 提交于 2019-12-01 23:24:42
I have gzipped data from HTTP reply. I have following code: def gzipDecode(self, content): import StringIO import gzip outFilePath = 'test' compressedFile = StringIO.StringIO(content) decompressedFile = gzip.GzipFile(fileobj=compressedFile) with open(outFilePath, 'w') as outfile: outfile.write(decompressedFile.read()) data = '' with open(outFilePath, 'r') as myfile: data=myfile.read().replace('\n', '') return data which decompress input gzipped content and return string (http reply is gzipped json). - It works. But I need it without creating test file - all in memory. I modified it to: def

What is the purpose of using StringIO in DecisionTree

二次信任 提交于 2019-12-01 22:29:15
问题 I am writing a decision tree and the following code is a part of the complete code: def show_tree(tree, features, path): f = io.StringIO() export_graphviz(tree, out_file=f, feature_names=features) pydotplus.graph_from_dot_data(f.getvalue()).write_png(path) img = misc.imread(path) plt.rcParams['figure.figsize'] = (20,20) plt.imshow(img) Could any one please tell me what is the purpose of using StringIO here? 回答1: Python is not my leading language, however I think answer for your question is

What is the purpose of using StringIO in DecisionTree

萝らか妹 提交于 2019-12-01 21:09:50
I am writing a decision tree and the following code is a part of the complete code: def show_tree(tree, features, path): f = io.StringIO() export_graphviz(tree, out_file=f, feature_names=features) pydotplus.graph_from_dot_data(f.getvalue()).write_png(path) img = misc.imread(path) plt.rcParams['figure.figsize'] = (20,20) plt.imshow(img) Could any one please tell me what is the purpose of using StringIO here? Python is not my leading language, however I think answer for your question is quite simple and does not require lot of research. StringIO is used here to maintain Input/Output text stream.

Python logging to StringIO handler

南楼画角 提交于 2019-12-01 03:08:23
I have a python test in which I want to test if the logging works properly. For example I have a function that creates a user and at the end the logging writes to log file the response. logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) handler = logging.handlers.WatchedFileHandler('mylogfile.log') formatter = logging.Formatter('%(asctime)s: %(message)s', '%d/%b/%Y:%H:%M:%S %z') handler.setFormatter(formatter) logger.addHandler(handler) logger.info('Some log text') In my test case I want to send the log output to the StringIO. class MyTest(unittest.TestCase): def setUp(self)

How can I pass a Python StringIO() object to a ZipFile(), or is it not supported?

主宰稳场 提交于 2019-11-30 23:13:38
问题 So I have a StringIO() file-like object, and I am trying to write it to a ZipFile(), but I get this TypeError: coercing to Unicode: need string or buffer, cStringIO.StringI found Here is a sample of the code I am using: file_like = StringIO() archive = zipfile.ZipFile(file_like, 'w', zipfile.ZIP_DEFLATED) # my_file is a StringIO object returned by a remote file storage server. archive.write(my_file) The docs say that StringIO() is a file-like class and that ZipFile() can accept a file-like

python 2.7 / exec / what is wrong?

心已入冬 提交于 2019-11-30 15:43:55
问题 I have this code which runs fine in Python 2.5 but not in 2.7: import sys import traceback try: from io import StringIO except: from StringIO import StringIO def CaptureExec(stmt): oldio = (sys.stdin, sys.stdout, sys.stderr) sio = StringIO() sys.stdout = sys.stderr = sio try: exec(stmt, globals(), globals()) out = sio.getvalue() except Exception, e: out = str(e) + "\n" + traceback.format_exc() sys.stdin, sys.stdout, sys.stderr = oldio return out print "%s" % CaptureExec(""" import random

python 2.7 / exec / what is wrong?

感情迁移 提交于 2019-11-30 15:08:14
I have this code which runs fine in Python 2.5 but not in 2.7: import sys import traceback try: from io import StringIO except: from StringIO import StringIO def CaptureExec(stmt): oldio = (sys.stdin, sys.stdout, sys.stderr) sio = StringIO() sys.stdout = sys.stderr = sio try: exec(stmt, globals(), globals()) out = sio.getvalue() except Exception, e: out = str(e) + "\n" + traceback.format_exc() sys.stdin, sys.stdout, sys.stderr = oldio return out print "%s" % CaptureExec(""" import random print "hello world" """) And I get: string argument expected, got 'str' Traceback (most recent call last):

How to pipe binary data into numpy arrays without tmp storage?

故事扮演 提交于 2019-11-30 04:37:42
问题 There are several similar questions but none of them answers this simple question directly: How can i catch a commands output and stream that content into numpy arrays without creating a temporary string object to read from? So, what I would like to do is this: import subprocess import numpy import StringIO def parse_header(fileobject): # this function moves the filepointer and returns a dictionary d = do_some_parsing(fileobject) return d sio = StringIO.StringIO(subprocess.check_output(cmd))