[笔记]python的StringIO与BytesIO模块

本文介绍如何利用Python中的StringIO和BytesIO模块在内存中读写字符串及二进制数据。通过示例代码展示了StringIO适用于处理纯文本,而BytesIO则用于处理二进制数据。
部署运行你感兴趣的模型镜像

StringIO

StringIO就是在内存中读写字符串,要把字符串读入内存,要先创建StringIO实例,然后像写入文件一样,将字符串写入这个实例即可。
>>> from StringIO import StringIO
>>> memory = StringIO()
>>> memory.write(‘hello’)
>>> memory.write(’ ‘)
>>> memory.write(‘world’)
>>> memory.getvalue()
‘hello world’

StringIO.getvalue()可以返回StringIO实例中的字符串。
如果要读取StringIO的字符串可以使用与文件类似的read,readline ,readlines等方法。

>>> memory.write(‘\n new line \n’)
>>> memory.seek(0) # 回到文件最开头
>>> for line in memory:
print line
hello world

new line

或者利用readline函数:
>>> memory.seek(0)
>>>

             while True:
                 s = memory.readline()
                 if s == '':
                      break
                print(s.strip())

hello world
new line

BytesIO

我们在读写文件时也有时利用二进制数据进行读取。
而StringIO只能读取字符串到内存,这时候,就需要BytesIO模块进行操作。
>>> from io import BytesIO
>>> a = unicode(‘你好世界!’,’gbk’)
>>> memory = BytesIO()
>>> memory.write(a.encode(‘utf8’))
15L
>>> print(memory.getvalue())
‘你好世界!’
>>> memory.seek(0)
0L
>>> memory.read()
‘\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81’
>>> a.encode(‘utf8’)
‘\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81’

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

Python 的 `StringIO` 模块(在 Python 3 中为 `io.StringIO`)允许在内存中模拟文件操作,这在单元测试中非常有用,特别是当需要测试依赖于文件读写的函数时,可以避免实际文件操作带来的复杂性和副作用。以下是使用 `StringIO` 模块进行单元测试的方法: ### 1. 测试写入操作 假设存在一个函数,其功能是将数据写入文件。可以使用 `StringIO` 模拟文件,然后验证写入的数据是否正确。 ```python from io import StringIO import unittest def write_to_file(file): file.write('Hello, World!') class TestWriteToFile(unittest.TestCase): def test_write_to_file(self): # 创建一个 StringIO 对象来模拟文件 fake_file = StringIO() # 调用要测试的函数 write_to_file(fake_file) # 获取写入的数据 result = fake_file.getvalue() # 验证写入的数据是否正确 self.assertEqual(result, 'Hello, World!') if __name__ == '__main__': unittest.main() ``` ### 2. 测试读取操作 若有一个函数从文件中读取数据,也可以使用 `StringIO` 模拟文件内容,然后验证读取的数据是否正确。 ```python from io import StringIO import unittest def read_from_file(file): return file.read() class TestReadFromFile(unittest.TestCase): def test_read_from_file(self): # 创建一个 StringIO 对象并设置初始内容 fake_file = StringIO('Hello, World!') # 调用要测试的函数 result = read_from_file(fake_file) # 验证读取的数据是否正确 self.assertEqual(result, 'Hello, World!') if __name__ == '__main__': unittest.main() ``` ### 3. 测试包含文件操作的函数 当函数中既有写入操作又有读取操作时,同样可以使用 `StringIO` 进行测试。 ```python from io import StringIO import unittest def process_file(file): file.write('Hello') file.seek(0) return file.read() class TestProcessFile(unittest.TestCase): def test_process_file(self): # 创建一个 StringIO 对象 fake_file = StringIO() # 调用要测试的函数 result = process_file(fake_file) # 验证处理结果是否正确 self.assertEqual(result, 'Hello') if __name__ == '__main__': unittest.main() ``` 在这些示例中,`StringIO` 对象被用作文件的替代品,从而避免了实际文件操作。通过 `getvalue()` 方法可以获取写入的数据,通过 `seek(0)` 方法可以将文件指针重置到文件开头以便读取数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matdodo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值