Python StringIO模块完全指南及示例
有时候我们需要将数据创建在内存中,而不是在操作系统中实际文件中读取或创建。这就是Python StringIO模块的用途。阅读本文详细了解Python StringIO模块。
Python StringIO模块是什么
Python StringIO模块是一个在内存中的类似文件的对象,可以用作大多数期望标准文件对象的输入和输出。换句话说,该类似文件的对象就像一个常规文件,允许进行大多数标准文件I/O操作。
它与常规文件之间的主要区别之一是,常规文件对操作系统可见,而文件对象是在内存中创建的,因此可以更高效地处理。
安装StringIO模块
StringIO模块包含在Python标准库的IO模块中,我们可以使用以下命令导入它 –
from io import StringIO
创建StringIO流/文件对象
我们可以通过向StringIO()模块传递一个字符串来创建StringIO流或文件对象。让我们通过下面给出的示例来理解。
from io import StringIO
String_doc = "Welcome to Tutorialspoint."
Object_string = StringIO(String_doc)
在上面的例子中,我们通过传递字符串类型的String_doc创建了一个名为Object_string的StringIO对象。
从StringIO对象中读取数据
我们可以使用read()函数来从StringIO对象中读取数据。下面给出了几行相关的代码。
示例
print(Object_string.read())
输出
输出结果如下:
Welcome to Tutorialspoint.
将数据写入StringIO对象
我们可以使用write()函数将数据写入StringIO对象。以下是几行相关的代码:
示例
Object_string.write(" The website is www.tutorialspoint.com.")
# making sure stream/file objcet is read from beginning
Object_string.seek(0)
print(Object_string.read())
输出
输出结果如下:
Welcome to Tutorialspoint. The website is www.tutorialspoint.com.
StringIO的重要方法
以下是StringIO的一些重要方法及其示例代码:
StringIO.getvalue()
顾名思义,此函数返回文件的全部内容。以下是函数的语法:
File_name.getvalue()
示例
from io import StringIO
# An arbitrary string
Object_string ="Welcome to TutorialsPoint."
# Using the StringIO method to set as file object
file_module = StringIO(Object_string)
# Retrieving the entire content of the file
print(file_module.getvalue())
输出
它将产生以下输出-
Welcome to TutorialsPoint.
StringIO.seek()
此函数用于设置文件的光标位置。一旦进行任何读写操作,光标就会设置在最后一个索引上。现在,要将光标移动到文件的起始索引处,我们可以使用seek()函数。
下面是seek()函数的语法−
File_name.seek(argument)
这里的参数将告诉函数在哪里设置光标位置。
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# Reading the file
print(file_module.read())
# But now if you want to read from the file again,
# it will show empty file because the cursor is
# set to the last index of the file.
# The below code will return an empty string and does not print anything.
print(file_module.read())
# Now to set the cursor position at start and to read/write file again,
# we can use the seek() function. We need to pass argument as well.
file_module.seek(0)
# Now we can read the file again
print(file_module.read())
输出
上述示例将输出以下内容:
Welcome to TutorialsPoint.
StringIO.truncate()
如字面意义所示,此函数调整文件流的大小。它会丢弃给定索引之后的内容,并将其保存。
以下是truncate()函数的语法:
File_name.truncate(size = None)
您可以提供从哪里截断文件的大小。
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# Reading the file
print(file_module.read())
# Now set the cursor at 0.
file_module.seek(0)
# Now we will drop the file after index 10.
file_module.truncate(10)
# Now we can read the file again after truncating
print(file_module.read())
输出
它将产生以下输出-
Welcome to TutorialsPoint.
Welcome to
StringIO.tell()
该函数告诉我们文件的当前光标位置。下面是tell()函数的语法:
File_name.tell()
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# The current cursor position is at 0
print(file_module.tell())
# Now set the cursor at 10.
file_module.seek(10)
# Print the index of cursor
print(file_module.tell())
输出
它将产生以下输出:
0
10
StringIO.close()
该函数关闭文件,一旦应用,我们就不能对该文件执行任何进一步的操作。如果您尝试执行任何操作,它将引发ValueError。
下面是close()函数的语法:
File_name.close()
示例
# Importing the StringIO module
from io import StringIO
# An arbitrary string
Object_string ="Welcome to TutorialsPoint."
# Using the StringIO method to set as file object
file_module = StringIO(Object_string)
# Reading the file
print(file_module.read())
# Closing the file
file_module.close()
# Let's try to read the file again
print(file_module.read())
输出
它将产生以下输出−
Welcome to TutorialsPoint.
-----------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-3c3241cbedca> in <module>
10 file_module.close()
11 # Reading the file
---> 12 print(file_module.read())
ValueError: I/O operation on closed file
StringIO布尔返回类型的函数
在这里,我们将讨论一些返回布尔值(即True或False)的StringIO方法 –
StringIO.isatty()
如果文件对象是交互式的,则此布尔函数将返回True;否则返回False。下面是isatty()函数的语法 –
File_name.isatty()
示例
# Importing the StringIO module
from io import StringIO
# An arbitrary string
Object_string ="Welcome to TutorialsPoint."
# Using the StringIO method to set as file object
file_module = StringIO(Object_string)
# It will return True if file is interactive
# and False if the file is not interactive.
print("Is the file object interactive?", file_module.isatty())
输出
它将产生以下输出-
Is the file stream interactive? False
StringIO.readable()
这个布尔函数将返回True,如果文件对象是可读的,并且如果文件对象不可读,则返回False。下面是readable()函数的语法−
File_name.readable()
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# It will return True if file is readable and
# False if the file is not readable.
print("Is the file readable?", file_module.readable())
输出
它将产生以下输出:
Is the file readable? True
StringIO.writable()
这个布尔函数会返回True,如果文件对象是可写的,并且如果文件对象是不可写的,则返回False。
以下是writable()函数的语法 −
File_name.writable()
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# It will return True if file is writeable and
# False if the file is not writeable.
print("Is the file writable?", file_module.writable())
输出
它将产生以下输出结果−
Is the file writable? True
StringIO.seekable()
这个布尔函数会在文件对象支持随机访问时返回True,如果文件对象不支持随机访问,则返回False。
seekable()函数的语法如下:
File_name.seekable()
示例
from io import StringIO
Object_string ="Welcome to TutorialsPoint."
file_module = StringIO(Object_string)
# It will return True if the file object supports random access and
# False if the file object does not support random access.
print("Is the file seekable?", file_module.seekable())
输出
它将产生以下输出-
Is the file seekable? True
StringIO.closed
如果文件对象已关闭,则此布尔函数将返回True;如果文件对象处于打开状态,则返回False。
示例
# Importing the StringIO module
from io import StringIO
# An arbitrary string
Object_string ="Welcome to TutorialsPoint."
# Using the StringIO method to set as file object
file_module = StringIO(Object_string)
# It will return True if file object supports random access
# and False if the file object does not support random access.
print("Is the file closed?", file_module.closed)
输出
它将产生以下输出 –
Is the file closed? False
结论
我们学习了Python中的StringIO模块,这是一个用于处理基于字符串的输入和输出流的有用工具。它允许我们创建一个类似文件的对象,可以用作各种函数的输入或输出,并且可以使用write()和read()方法轻松地读取和写入。
使用StringIO的主要好处之一是它允许您与文件类似地处理字符串。当您需要从字符串读取或写入字符串时,这特别有用,但又不想在磁盘上创建临时文件。
StringIO的另一个优点是它非常高效,因为它将数据存储在内存中而不是磁盘上。对于小量数据,它比使用实际文件更快。
总体而言,StringIO是Python中处理基于字符串的输入和输出流的一个有用且高效的工具。它易于使用,并且可以作为处理实际文件的方便替代方法。我们还了解了Python StringIO模块及其方法在处理文件流方面的几种有效用例。