标签: stringio

将StringIO的内容写入文件的最佳方法是什么?

StringIO缓冲区的内容写入文件的最佳方法是什么?

我目前做的事情如下:

buf = StringIO()
fd = open ('file.xml', 'w')
# populate buf
fd.write (buf.getvalue ())
Run Code Online (Sandbox Code Playgroud)

但那么buf.getvalue ()会复制内容吗?

python file-io stringio

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

我必须做StringIO.close()吗?

一些代码:

import cStringIO

def f():
    buffer = cStringIO.StringIO()
    buffer.write('something')
    return buffer.getvalue()
Run Code Online (Sandbox Code Playgroud)

文件说:

StringIO.close():释放内存缓冲区.尝试使用已关闭的StringIO对象执行进一步操作将引发ValueError.

我是否必须这样做buffer.close(),否则当缓冲区超出范围并被垃圾收集时会自动发生?

更新:

我做了一个测试:

import StringIO, weakref

def handler(ref):
    print 'Buffer died!'

def f():
    buffer = StringIO.StringIO()
    ref = weakref.ref(buffer, handler)
    buffer.write('something')
    return buffer.getvalue()

print 'before f()'
f()
print 'after f()'
Run Code Online (Sandbox Code Playgroud)

结果:

vic@wic:~/projects$ python test.py 
before f()
Buffer died!
after f()
vic@wic:~/projects$
Run Code Online (Sandbox Code Playgroud)

python stringio

27
推荐指数
3
解决办法
1万
查看次数

Python:如何让StringIO.writelines接受unicode字符串?

我得到了一个

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 34: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

存储在'a.desc'中的字符串,因为它包含'£'字符.它作为unicode字符串存储在底层Google App Engine数据存储区中,因此没问题.cStringIO.StringIO.writelines函数试图似乎试图以ascii格式对其进行编码:

result.writelines(['blahblah',a.desc,'blahblahblah'])
Run Code Online (Sandbox Code Playgroud)

如果这是正确的措辞,我如何指示它将编码视为unicode?

app引擎在python 2.5上运行

python string unicode ascii stringio

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

如何在Python 2.7中使用StringIO解决TypeError?

尝试使用以下字符串作为文件读取以下字符串StringIO.我该如何解决?

>> from io import StringIO
>>>
>>> datastring = StringIO("""\
... Country  Metric           2011   2012   2013  2014
... USA     GDP               7      4     0      2
... USA     Pop.              2      3     0      3
... GB      GDP               8      7     0      7
... GB      Pop.              2      6     0      0
... FR      GDP               5      0     0      1
... FR      Pop.              1      1     0      5
... """)
Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
TypeError: initial_value must be unicode …
Run Code Online (Sandbox Code Playgroud)

python stringio python-2.7

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

混淆StringIO,cStringIO和ByteIO

我用google搜索并在SO上搜索这些缓冲模块之间的区别.但是,我仍然不太了解,我认为我读到的一些帖子已经过时了.

在Python 2.7.11中,我使用了下载了特定格式的二进制文件r = requests.get(url).然后我通过StringIO.StringIO(r.content),cStringIO.StringIO(r.content)io.BytesIO(r.content)设计用于解析内容的功能.

所有这三种方法都可用.我的意思是,即使文件是二进制文件,它仍然可以使用StringIO.为什么?

另一件事是他们的效率.

In [1]: import StringIO, cStringIO, io

In [2]: from numpy import random

In [3]: x = random.random(1000000)

In [4]: %timeit y = cStringIO.StringIO(x)
1000000 loops, best of 3: 736 ns per loop

In [5]: %timeit y = StringIO.StringIO(x)
1000 loops, best of 3: 283 µs per loop

In [6]: %timeit y = io.BytesIO(x)
1000 loops, best of 3: 1.26 ms per loop
Run Code Online (Sandbox Code Playgroud)

如上图所示 …

python bytesio stringio cstringio

22
推荐指数
1
解决办法
1万
查看次数

Python的StringIO与`with`语句不相符

我需要存根tempfile并且StringIO看起来很完美.只有这一切都失败了:

In [1]: from StringIO import StringIO
In [2]: with StringIO("foo") as f: f.read()

--> AttributeError: StringIO instance has no attribute '__exit__'
Run Code Online (Sandbox Code Playgroud)

提供固定信息而不是读取具有不确定内容的文件的常用方法是什么?

python unit-testing stringio stubs

19
推荐指数
1
解决办法
5537
查看次数

如何从内存缓冲区(StringIO)或使用opencv python库的url读取映像

只需共享一种从内存缓冲区或url创建opencv图像对象的方法,以提高性能.

有时我们会遇到图像二值从网址,以避免额外的文件IO,我们要imread从内存缓冲或网址这一形象,但imread只支持从路径文件系统中读取图像.

opencv numpy image urllib2 stringio

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

Python Flask send_file StringIO空白文件

我正在使用python 3.5和flask 0.10.1并且喜欢它,但是在send_file上遇到了一些麻烦.我最终想要处理一个pandas数据帧(来自Form数据,在本例中未使用但将来是必需的)并将其作为csv(没有临时文件)发送到下载.我见过的最好的方法是给我们StringIO.

这是我试图使用的代码:

@app.route('/test_download', methods = ['POST'])
def test_download():
    buffer = StringIO()
    buffer.write('Just some letters.')
    buffer.seek(0)
    return send_file(buffer, as_attachment = True,\
    attachment_filename = 'a_file.txt', mimetype = 'text/csv')
Run Code Online (Sandbox Code Playgroud)

使用正确的名称下载文件,但文件完全空白.

有任何想法吗?编码问题?有没有在其他地方回答过?谢谢!

python stringio flask

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

将io.BytesIO转换为io.StringIO以解析HTML页面

我正在尝试解析通过pyCurl检索的HTML页面,但是pyCurl WRITEFUNCTION将页面返回为BYTES而不是字符串,所以我无法使用BeautifulSoup解析它.

有没有办法将io.BytesIO转换为io.StringIO?

或者还有其他方法来解析HTML页面吗?

我正在使用Python 3.3.2.

html beautifulsoup type-conversion pycurl stringio

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

我可以像StringIO一样使用cStringIO吗?

我这样做了:

import cStringIO.StringIO as StringIO
Run Code Online (Sandbox Code Playgroud)

我意识到我到处都在使用它.那很好吗?它与StringIO一样对待吗?

c python stringio

17
推荐指数
2
解决办法
7435
查看次数