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电脑。
