Python中是否存在C#MemoryStream
的某些模拟(可以让我将二进制数据从某些源直接写入内存)?我将如何去使用它?Python中的MemoryStream模拟
6
A
回答
10
StringIO的是一种可能性:http://docs.python.org/library/stringio.html
此模块实现一个类文件的类,
StringIO
,读取和写入字符串缓冲区中(也称为存储器文件)。请参阅文件对象的操作说明(部分文件对象)。 (对于标准的字符串,见str
和unicode
。)...
+3
或'cStringIO',这是相同的,但在C中以速度实现。 – 2010-11-18 15:47:16
3
如果您正在使用Python> = 3.0,尝试了Adam's answer,你会发现,import StringIO
或import cStringIO
都给出一个导入错误。这是因为StringIO是now part of the io
module。
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!
...
您可以使用StringIO
就好像它是一个普通的Python文件:write()
,close()
,和所有爵士乐,另外还有getvalue()
检索字符串。
相关问题
- 1. 的MemoryStream到虚拟文件
- 2. Python中的devar的模拟
- 3. 模拟Python中的列表
- 4. python中的同时模拟
- 5. Unix的Python模拟''
- 6. Python pthread_detach模拟
- 7. python中有getch()模拟吗?
- 8. 从模拟补丁/ Python中
- 9. 在Python中模拟C宏
- 10. Python模拟测试模拟会话
- 11. Python的OrderedDict的模拟?
- 12. 去Python的fileinput.input()的模拟?
- 13. Python的defaultdict的模拟?
- 14. Python签出模拟
- 15. Python BASIC模拟器
- 16. rvmsudo模拟python/virtualenv
- 17. Python骰子模拟
- 18. 模拟时间python
- 19. Monty Hall Python模拟
- 20. Python:在Pexpect模块中模拟re.findall
- 21. 在Python中模拟Ising模型
- 22. 如何在unittest中模拟python模块
- 23. 模拟Python交互模式
- 24. Python模拟jQuery的网页
- 25. Python的模拟返回值
- 26. 公司的Python模拟
- 27. Python的IRB模拟-i
- 28. Groovy的Python模拟器吗?
- 29. Python的模拟工作不
- 30. Python的模拟返回值
你问'StringIO'吗? – 2010-11-18 15:34:59