标签: stringio

scikit-image 将图像保存为字节串

我正在使用scikit-image读取图像:

img = skimage.io.imread(filename)
Run Code Online (Sandbox Code Playgroud)

对 进行一些操作后img,我想将它保存到内存文件(a la StringIO)以传递给另一个函数,但它看起来skimage.io.imsave需要文件名,而不是文件句柄。

imsave如果可能的话,我想避免击中磁盘(然后从另一个成像库读取)。有没有一种很好的方法来使用imsave(或其他一些 scikit-image-friendly 功能)StringIO

python in-memory stringio scikit-image

5
推荐指数
1
解决办法
2741
查看次数

将 ZipFile 从 URL 读取到 StringIO 中并使用 panda.read_csv 进行解析

ZipFile我正在尝试从 URL读取数据并通过使用解析 as csvStringIO内的数据ZipFilepandas.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)
Run Code Online (Sandbox Code Playgroud)

最后一行给了我一个错误:

CParserError: Error tokenizing data. C error: Calling read(nbytes) on source failed. Try engine='python'.
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试使用

salaries = pd.read_csv(file.open("Salaries.csv"))
Run Code Online (Sandbox Code Playgroud)

有用。

所以我想知道我在这里错过了什么。

file.open应该返回一个ZipExtFile对象,并且由于 read_csv 仅接受字符串或文件句柄/StringIO输入,为什么最后一行可以工作?

python stringio pandas python-zipfile

5
推荐指数
2
解决办法
6159
查看次数

Python在内存中解压gzip数据,无需文件

我已经从 HTTP 回复中压缩了数据。我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

它解压缩输入的 gzip 压缩内容并返回字符串(http 回复是 gzip 压缩的 json)。- 有用。

但我需要它而不创建测试文件 - 全部在内存中。

我将其修改为:

def gzipDecode(self, content):
    import StringIO
    from io import BytesIO
    import gzip

    outFile = StringIO.StringIO()

    compressedFile = StringIO.StringIO(content)
    decompressedFile = gzip.GzipFile(fileobj=compressedFile)

    outFile.write(decompressedFile.read())
    outFile.flush()

    data = outFile.read().replace('\n', '')
    print "_" + …
Run Code Online (Sandbox Code Playgroud)

python compression json gzip stringio

5
推荐指数
1
解决办法
3150
查看次数

tempfile模块和IO文件类对象有什么区别

io.BytesIO()我发现这两个模块在使用or创建临时文件方面有很多相似之处,io.StringIo()每个tempfile.TemporaryFile() 模块的用途是什么?

temporary-files bytesio stringio python-3.x

5
推荐指数
1
解决办法
3301
查看次数

Python的Clojure的StringIO

有没有相当于Python的StingIO for Clojure的东西?

我正在尝试编写一个类似于SweavePweave for Clojure 的报告生成/识字编程系统.我目前正在使用临时文件,但我更喜欢使用类似于StringIO的东西.

python clojure stringio

4
推荐指数
1
解决办法
349
查看次数

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

我假设sys.stdout将引用与在同一进程中运行的iostreams :: cout相同的物理流,但似乎并非如此.下面的代码使用名为"write"的python包装器调用C++函数,该函数写入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 
print stringout.getvalue() 
Run Code Online (Sandbox Code Playgroud)

立即将"cout"写入控制台,然后分隔符"---...",最后,作为stringout.getvalue()的返回值,字符串"stdout".我的目的是在stringout中捕获从C++写入cout的字符串.有谁知道发生了什么,如果有的话,我怎么能捕获python字符串中写入cout的内容?

提前致谢.

c++ python stdout stringio

4
推荐指数
1
解决办法
4705
查看次数

Ruby Mock是一个使用StringIO的文件

我试图在Ruby的StringIO的帮助下模拟 文件读取.以下是我的测试,接下来就是我在主类中的方法.

def test_get_symbols_from_StringIO_file
    s = StringIO.new("YHOO,141414")
    assert_equal(["YHOO,141414"], s.readlines)
end

def get_symbols_from_file (file_name)
  IO.readlines(file_name, ',')
end
Run Code Online (Sandbox Code Playgroud)

我想知道这是否是我们模拟文件读取的方式,而且我想知道是否有其他方法来模拟类中的方法而不是使assert与内容相等.

ruby file-io mocking stringio

4
推荐指数
1
解决办法
3903
查看次数

python PIL图像如何将图像保存到缓冲区,以便以后使用?

我有一个png应该转换为jpg并保存到的文件gridfs,我使用python的PILlib加载该文件并执行转换工作,问题是我想将转换后的图像存储到MongoDB Gridfs中,在保存过程中,我可以只需使用该im.save()方法。所以我用a StringIO来保存临时文件,但是不起作用。

这是代码片段:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image
from pymongo import MongoClient
import gridfs
from StringIO import StringIO


im = Image.open("test.png").convert("RGB")

#this is what I tried, define a 
#fake_file with StringIO that stored the image temporarily.
fake_file = StringIO()
im.save(fake_file,"jpeg")

fs = gridfs.GridFS(MongoClient("localhost").stroage)

with fs.new_file(filename="test.png") as fp:
    # this will not work
    fp.write(fake_file.read())


# vim:ai:et:sts=4:sw=4:
Run Code Online (Sandbox Code Playgroud)

我对python的IO机制非常了解,如何使这项工作有效?

python io stringio gridfs

4
推荐指数
1
解决办法
5105
查看次数

python3打印到字符串

使用Python 3,我有一个控制台应用程序,我正在移植到GUI.代码有一堆打印语句,如下所示:

print(' ', 'p', ' ', sep='', end='')

(实际上,位置参数实际上是从函数返回的,并且可能有多于或少于3,如下所示:

print(f1(), f2(), f3(), sep='', end='')

我想将这些调用转换为:

GuiPrintLine(' ', 'p', ' ', sep='', end='')

其中每一行最终使用GUI框架呈现.

如果我可以将参数转换为字符串,这很容易做到.我想将参数列表格式化为单个字符串,打印通常会输出如下:

def GuiPrintLine(*args, **kwargs):
    s = text.format(*args, **kwargs)
    # ... then display the string on the gui ...
Run Code Online (Sandbox Code Playgroud)

但是,这需要格式字符串(文本).有一种简单的方法可以自动生成与打印功能相同的输出吗?我意识到我可以通过用sep和end结束所有args来模拟打印,但我更愿意使用内置的解决方案(如果有的话).

使用打印和重定向sysout是不具吸引力的,因为它需要修改应用程序的全局状态,sysout可能同时用于其他诊断.

看起来这在Python中应该是微不足道的,所以也许我只是遗漏了一些明显的东西.

谢谢你的帮助!

python string stringio python-3.x

4
推荐指数
1
解决办法
2206
查看次数

StringIO中的就地替换

如何在a内用另一个替换字符串StringIO?-我听说它们的长度相同是可能的。

尝试:

from cStringIO import StringIO

c = 'can\nhaz\nfoo'

sio = StringIO(c)

for line in sio:
    if line == 'haz\n':
        # sio.write('bar\n')
        line = 'bar\n'
        break

sio.seek(0)
sio.readlines()   # [ 'can\n', 'haz\n', 'bar' ]
Run Code Online (Sandbox Code Playgroud)

PS:目前正在使用C解决方案,但宁愿进行这项工作。

python stringio python-2.7 python-internals cstringio

4
推荐指数
1
解决办法
1611
查看次数