stringio

python sys.stdout and C++ iostreams::cout

六月ゝ 毕业季﹏ 提交于 2021-02-07 14:30:31
问题 I was assuming that sys.stdout would be referencing the same physical stream as iostreams::cout running in the same process, but this doesn't seem to be the case. The following code, which makes a call to a C++ function with a python wrapper called "write", that writes to cout: from cStringIO import StringIO import sys orig_stdout = sys.stdout sys.stdout = stringout = StringIO() write("cout") # wrapped C++ function that writes to cout print "-" * 40 print "stdout" sys.stdout = orig_stdout

python sys.stdout and C++ iostreams::cout

那年仲夏 提交于 2021-02-07 14:27:14
问题 I was assuming that sys.stdout would be referencing the same physical stream as iostreams::cout running in the same process, but this doesn't seem to be the case. The following code, which makes a call to a C++ function with a python wrapper called "write", that writes to cout: from cStringIO import StringIO import sys orig_stdout = sys.stdout sys.stdout = stringout = StringIO() write("cout") # wrapped C++ function that writes to cout print "-" * 40 print "stdout" sys.stdout = orig_stdout

How to fix “ImportError: cannot import name 'StringIO'”

旧时模样 提交于 2021-02-07 12:40:43
问题 python version is 3.6.6 and pandas_datareader version is 0.7.0 when i import pandas_datareader, an error occurs like below. C:\PycharmProjects\Demo\venv\Scripts\python.exe C:/PycharmProjects/Demo/stock.py Traceback (most recent call last): File "C:/PycharmProjects/Demo/stock.py", line 3, in <module> import pandas_datareader as wb File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module> from .data import (DataReader, Options, get_components_yahoo

How to fix “ImportError: cannot import name 'StringIO'”

☆樱花仙子☆ 提交于 2021-02-07 12:40:11
问题 python version is 3.6.6 and pandas_datareader version is 0.7.0 when i import pandas_datareader, an error occurs like below. C:\PycharmProjects\Demo\venv\Scripts\python.exe C:/PycharmProjects/Demo/stock.py Traceback (most recent call last): File "C:/PycharmProjects/Demo/stock.py", line 3, in <module> import pandas_datareader as wb File "C:\PycharmProjects\Demo\venv\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module> from .data import (DataReader, Options, get_components_yahoo

Read ZipFile from URL into StringIO and parse with panda.read_csv

别说谁变了你拦得住时间么 提交于 2020-12-06 06:59:12
问题 I'm trying to read ZipFile data from a URL and via StringIO parse the data inside the ZipFile as csv using pandas.read_csv r = req.get("http://seanlahman.com/files/database/lahman-csv_2014-02-14.zip").content file = ZipFile(StringIO(r)) salaries_csv = file.open("Salaries.csv") salaries = pd.read_csv(salaries_csv) The last line gave me an error: CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'. However if i try using salaries = pd.read

How to write a string to file on ftp with pysftp?

北战南征 提交于 2020-06-26 04:15:23
问题 I have a large xml file stored in a variable. I want to write it directly to an ftp using pysftp. I believe I need to use the pysftp.putfo and this needs a file like object. Here is a minimal example: from io import StringIO from pysftp import Connection, CnOpts cnopts = CnOpts() cnopts.hostkeys = None with Connection('localhost' ,username= 'admin' ,password = 'test' ,cnopts=cnopts ) as sftp: sftp.putfo(StringIO('xml string')) I get the following error: TypeError: Expected unicode or bytes,

tempfile.TemporaryFile vs. StringIO

◇◆丶佛笑我妖孽 提交于 2020-06-25 03:39:13
问题 I've written a little benchmark where i compare different string concatenating methods for ZOCache. So it looks here like tempfile.TemporaryFile is faster than anything else: $ python src/ZOCache/tmp_benchmark.py 3.00407409668e-05 TemporaryFile 0.385630846024 SpooledTemporaryFile 0.299962997437 BufferedRandom 0.0849719047546 io.StringIO 0.113346099854 concat The benchmark code i've been using: #!/usr/bin/python from __future__ import print_function import io import timeit import tempfile

Python: simulate writing to a file object without creating a file

不问归期 提交于 2020-05-09 06:03:47
问题 I'm working with Python3 and I want to simulate writing to a file, but without actually creating a file. For example, my specific case is as follows: merger = PdfFileMerger() for pdf in files_to_merge: merger.append(pdf) merger.write('result.pdf') # This creates a file. I want to avoid this merger.close() # pdf -> binary with open('result.pdf', mode='rb') as file: # Conversely. I don't want to read the data from an actual file file_content = file.read() I think StringIO is a good candidate

Python: simulate writing to a file object without creating a file

放肆的年华 提交于 2020-05-09 06:03:08
问题 I'm working with Python3 and I want to simulate writing to a file, but without actually creating a file. For example, my specific case is as follows: merger = PdfFileMerger() for pdf in files_to_merge: merger.append(pdf) merger.write('result.pdf') # This creates a file. I want to avoid this merger.close() # pdf -> binary with open('result.pdf', mode='rb') as file: # Conversely. I don't want to read the data from an actual file file_content = file.read() I think StringIO is a good candidate

python PIL image how to save image to a buffer so can be used later?

拥有回忆 提交于 2020-01-21 07:49:05
问题 I have a png file which should be convert to jpg and save to gridfs , I use python's PIL lib to load the file and do the converting job, the problem is I want to store the converted image to a MongoDB Gridfs, in the saving procedure, I can't just use the im.save() method. so I use a StringIO to hold the temp file but it don't work. here is the code snippet: #!/usr/bin/env python # -*- coding: utf-8 -*- from PIL import Image from pymongo import MongoClient import gridfs from StringIO import