如何使用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) 如何使用字符串的内容创建类似文件的对象(与文件类似的鸭子类型)?
使用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
类,一个可以使用bytes
而不是字符串?
这可能不是很明显,但如果您使用StringIO处理二进制数据,那么您将无法使用Python 2.7或更新版本.
我创建了一个stringio对象,其中包含一些文本.我想清除它现有的值并重用它而不是回忆它.无论如何这样做?
我有一个virtualenv运行python 3.4.0点子版本是pip 1.5.4我做pip安装电子邮件并得到错误:ImportError:在进程结束时没有名为'cStringIO'的模块(失败)如何获取电子邮件用于python 3.4.0的包
使用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) 何时使用Ruby的StringIO而不是仅仅使用String?
我想我明白他们之间的根本区别的是突出了" 什么是Ruby的StringIO类,是真的吗? ",这StringIO的使人们能够读取和/写入到一个面向流的方式的字符串.但这实际意味着什么呢?
简单地使用String时使用StringIO实际使用的一个很好的例子是不是真的会削减它?
如何将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) 我想使用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)