繁体   English   中英

Python中的MemoryStream模拟

[英]MemoryStream analog in Python

Python中是否存在一些C# MemoryStream模拟(可以让我将某些源的二进制数据直接写入内存)? 我将如何使用它?

StringIO是一种可能性: http ://docs.python.org/library/stringio.html

该模块实现了一个类文件类StringIO ,它读写字符串缓冲区(也称为内存文件 )。 请参阅操作的文件对象的描述(部分文件对象 )。 (对于标准字符串,请参阅strunicode 。)...

如果您使用的是Python> = 3.0并尝试了Adam的答案 ,您会注意到import StringIOimport cStringIO都会导致导入错误。 这是因为StringIO 现在是io模块的一部分

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import StringIO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'StringIO'
>>> # Huh? Maybe this will work...
... 
>>> import cStringIO
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cStringIO'
>>> # Whaaaa...?
... 
>>> import io
>>> io.StringIO
<class '_io.StringIO'>
>>> # Oh, good!
... 

您可以像使用常规Python文件一样使用StringIOwrite()close()和所有jazz,并使用额外的getvalue()来检索字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM