标签: stringio

检索subprocess.call()的输出

如何使用subprocess.call()?获取进程的输出?

传递StringIO.StringIO对象stdout会出现此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles
    c2pwrite = stdout.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>> 
Run Code Online (Sandbox Code Playgroud)

python subprocess pipe stringio

248
推荐指数
7
解决办法
36万
查看次数

如何在Python中将字符串包装在文件中?

如何使用字符串的内容创建类似文件的对象(与文件类似的鸭子类型)?

python string file stringio

75
推荐指数
4
解决办法
4万
查看次数

无法在python中使用StringIO的read()获取数据

使用Python2.7版本.以下是我的示例代码.

import StringIO
import sys

buff = StringIO.StringIO()
buff.write("hello")
print buff.read()
Run Code Online (Sandbox Code Playgroud)

在上面的程序中,read()不返回任何内容,因为getvalue()返回"hello".任何人都可以帮我解决问题吗?我需要read(),因为我的以下代码涉及读取"n"字节.

python stringio

64
推荐指数
2
解决办法
2万
查看次数

使用字节而不是字符串的Python StringIO替换?

有没有替代python StringIO类,一个可以使用bytes而不是字符串?

这可能不是很明显,但如果您使用StringIO处理二进制数据,那么您将无法使用Python 2.7或更新版本.

python unicode stringio python-2.7

63
推荐指数
3
解决办法
4万
查看次数

如何清除stringio对象?

我创建了一个stringio对象,其中包含一些文本.我想清除它现有的值并重用它而不是回忆它.无论如何这样做?

python stringio

54
推荐指数
2
解决办法
3万
查看次数

python 3.4.0 email package install:ImportError:没有名为'cStringIO'的模块

我有一个virtualenv运行python 3.4.0点子版本是pip 1.5.4我做pip安装电子邮件并得到错误:ImportError:在进程结束时没有名为'cStringIO'的模块(失败)如何获取电子邮件用于python 3.4.0的包

stringio python-3.x cstringio

52
推荐指数
2
解决办法
7万
查看次数

何时使用StringIO,而不是加入字符串列表?

使用StringIO作为字符串缓冲区比使用list作为缓冲区慢.

何时使用StringIO?

from io import StringIO


def meth1(string):
    a = []
    for i in range(100):
        a.append(string)
    return ''.join(a)

def meth2(string):
    a = StringIO()
    for i in range(100):
        a.write(string)
    return a.getvalue()


if __name__ == '__main__':
    from timeit import Timer
    string = "This is test string"
    print(Timer("meth1(string)", "from __main__ import meth1, string").timeit())
    print(Timer("meth2(string)", "from __main__ import meth2, string").timeit())
Run Code Online (Sandbox Code Playgroud)

结果:

16.7872819901
18.7160351276
Run Code Online (Sandbox Code Playgroud)

python stringio

48
推荐指数
3
解决办法
6万
查看次数

在Ruby中使用StringIO而不是String有什么好处?

何时使用Ruby的StringIO而不是仅仅使用String?

我想我明白他们之间的根本区别的是突出了" 什么是Ruby的StringIO类,是真的吗? ",这StringIO的使人们能够读取和/写入到一个面向流的方式的字符串.但这实际意味着什么呢?

简单地使用String时使用StringIO实际使用的一个很好的例子是不是真的会削减它?

ruby string stringio

45
推荐指数
2
解决办法
3万
查看次数

将zipfile解压缩到内存?

如何将zip解压缩到内存中?

我尝试(返回None.getvalue()):

from zipfile import ZipFile
from StringIO import StringIO

def extract_zip(input_zip):
    return StringIO(ZipFile(input_zip).extractall())
Run Code Online (Sandbox Code Playgroud)

python memory zip zipfile stringio

42
推荐指数
2
解决办法
3万
查看次数

下载并解压缩内存中的gzip压缩文件?

我想使用urllib下载文件并在保存之前将文件解压缩到内存中.

这就是我现在所拥有的:

response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO()
compressedFile.write(response.read())
decompressedFile = gzip.GzipFile(fileobj=compressedFile, mode='rb')
outfile = open(outFilePath, 'w')
outfile.write(decompressedFile.read())
Run Code Online (Sandbox Code Playgroud)

这最终会写出空文件.我怎样才能实现我追求的目标?

更新答案:

#! /usr/bin/env python2
import urllib2
import StringIO
import gzip

baseURL = "https://www.kernel.org/pub/linux/docs/man-pages/"        
# check filename: it may change over time, due to new updates
filename = "man-pages-5.00.tar.gz" 
outFilePath = filename[:-3]

response = urllib2.urlopen(baseURL + filename)
compressedFile = StringIO.StringIO(response.read())
decompressedFile = gzip.GzipFile(fileobj=compressedFile)

with open(outFilePath, 'w') as outfile:
    outfile.write(decompressedFile.read())
Run Code Online (Sandbox Code Playgroud)

python gzip file urllib2 stringio

38
推荐指数
3
解决办法
4万
查看次数