笔记 - python:StringIO / BytesIO

本文介绍Python中StringIO和BytesIO模块的使用,演示了如何在内存中读写字符串和字节,类似于文件操作但无需磁盘I/O,适用于高效数据处理。

参考:

廖雪峰python的StringIO模块
python的StringIO模块


本质上就是在内存里操作字符串或者字节嘛,只不过看起来像在操作文件

StringIO

"""
read()等类似文件的接口,使用的时候和文件很像,==光标== 也会移动
而且只能读取初始化时加载的数据,不能读取后面 write 的数据
"""

from io import StringIO

f1 = StringIO()
f1.write("hello")
print(f1.getvalue())
print(f1.read())
print(f1.getvalue())
print("-"*20)
f2 = StringIO("hello world")
print(f2.read())
print(f2.getvalue())
print("read之后")
print(f2.read())
print("read之后")
f2.write("!")
print(f2.read())
print(f2.getvalue())
print("end")
hello

hello
--------------------
hello world
hello world
read之后

read之后

hello world!
end

BytesIO

>>> from io import BytesIO
>>> f = BytesIO()
>>> f.write('中文'.encode('utf-8'))
6
>>> print(f.getvalue())
b'\xe4\xb8\xad\xe6\x96\x87'

--------------------------

>>> from io import BytesIO
>>> f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')
>>> f.read()
b'\xe4\xb8\xad\xe6\x96\x87'
jcfh@ubuntu:~/Desktop/yolov5-6.01$ pip3 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn Defaulting to user installation because normal site-packages is not writeable Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting matplotlib>=3.2.2 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/22/d4/e7ca532e68a9357742604e1e4ae35d9c09a4a810de39a9d80402bd12f50f/matplotlib-3.3.4.tar.gz (37.9 MB) Preparing metadata (setup.py) ... error ERROR: Command errored out with exit status -4: command: /usr/bin/python3 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kd8xrooo/matplotlib_96ec52eb519e4b3191d5cff677be9ad2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kd8xrooo/matplotlib_96ec52eb519e4b3191d5cff677be9ad2/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-rzqaz1m3 cwd: /tmp/pip-install-kd8xrooo/matplotlib_96ec52eb519e4b3191d5cff677be9ad2/ Complete output (19 lines): Edit setup.cfg to change the build options; suppress output with --quiet. BUILDING MATPLOTLIB matplotlib: yes [3.3.4] python: yes [3.6.9 (default, Mar 10 2023, 16:46:00) [GCC 8.4.0]] platform: yes [linux] sample_data: yes [installing] tests: no [skipping due to configuration] macosx: no [Mac OS-X only] running egg_info creating /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info writing /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/PKG-INFO writing dependency_links to /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/dependency_links.txt writing namespace_packages to /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/namespace_packages.txt writing requirements to /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/requires.txt writing top-level names to /tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/top_level.txt writing manifest file '/tmp/pip-pip-egg-info-rzqaz1m3/matplotlib.egg-info/SOURCES.txt' ---------------------------------------- WARNING: Discarding https://pypi.tuna.tsinghua.edu.cn/packages/22/d4/e7ca532e68a9357742604e1e4ae35d9c09a4a810de39a9d80402bd12f50f/matplotlib-3.3.4.tar.gz#sha256=3e477db76c22929e4c6876c44f88d790aacdf3c3f8f3a90cb1975c0bf37825b0 (from https://pypi.tuna.tsinghua.edu.cn/simple/matplotlib/) (requires-python:>=3.6). Command errored out with exit status -4: python setup.py egg_info Check the logs for full command output.
最新发布
09-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值