当前位置: 首页 > 图灵资讯 > 行业资讯> python中StringIO的读写

python中StringIO的读写

发布时间:2024-08-27 13:27:15

1、概念

StringIO是在内存中读写Str。

在StringIO中写stringIO,首先要创建StringIO,然后像文件一样写:

>>>fromioimportStringIO
>>>f=StringIO()
>>>f.write('hello')
5
>>>f.write('')
1
>>>f.write('world!')
6
>>>print(f.getvalue())
helloworld!

2、为了读取StringIO,可以用StringIO初始化,然后像读取文件一样读取:

>>>fromioimportStringIO
>>>f=StringIO('Hello!\nHi!\nGoodbye!')
>>>whileTrue:
...s=f.readline()
...ifs=='':
...break
...print(s.strip())
...
Hello!
Hi!
Goodbye!

以上是python中StringIO的读写,希望对大家有所帮助。更多Python学习推荐:python教学

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12