Python 中的 StringIO 模块
这个 StringIO 模块是一个内存中的类似文件的对象。它可以用来输入或输出大多数普通文件对象所能期望的函数。一旦用户创建了 StringIO 对象,它会通过将一个字符串提供给构造函数来进行初始化。如果没有字符串,StringIO 将是空的。在这两种情况下,文件上初始显示的光标位置将从零开始。
该模块在最近的 Python 版本中不可用;因此,为了能够使用这个模块,我们需要将其转换为 Python 中的 io.StringIO 形式并引入 Io 模块。
示例:
# First, we will import the required module.
from io import StringIO as SIO
# The arbitrary string.
string_1 = 'This is the initialized string.'
# Here, we will use the StringIO method for setting as the file object.
# Now, we have an object-file that we can treat as a file.
file_1 = SIO(string_1)
# Now, we will be reading the file by using read()
print (file_1.read())
# Here, We can also write in this file.
file_1.write(" Welcome to Javatpoint.com.")
# by using the following command, we can make the cursor at index 0.
file_1.seek(0)
# by using the following command, the user is able to print the file after writing #in the initialized string (string_1).
print ('The file of the string after writing in it is:', file_1.read())
输出:
This is the initialized string.
The file of the string after writing in it is: This is the initialized string. Welcome to Javatpoint.com.
StringIO的重要方法
以下是一些StringIO的方法:
1. StringIO.getvalue(): 该函数用于返回文件的全部内容。
语法:
以上方法的语法如下:
File_name.getvalue()
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 = 'Hello and thank you for visiting to Javatpoint.com.'
# Here, we will use the StringIO method for setting as the file object.
file_1 = SIO(string_1)
# Retrieving the complete contents of the above file.
print(file_1.getvalue())
输出:
Hello and thank you for visiting to Javatpoint.com
2. 在这里,我们看一下StringIO的一些返回布尔值(即false或true)的函数:
- isatty(): 如果流不是交互式的,则StringIO的此函数返回False,如果流是交互式的,则返回True。
- readable(): 如果文件不可读,则StringIO的此函数返回False,如果文件可读,则返回True。
- writable(): 如果文件不支持写入,则StringIO的此函数返回False,如果文件支持写入,则返回True。
- seekable(): 如果文件不支持随机访问,则StringIO的此函数返回False,如果文件支持随机访问,则返回True。
- closed: 如果文件已打开,则StringIO的此函数返回False,如果文件已关闭,则返回True。
语法:
以上方法的语法如下:
1. File_name.isatty()
2. File_name.readable()
3. File_name.writable()
4. File_name.seekable()
5. File_name.closed
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 = 'Hello and thank you for visiting to Javatpoint.com.'
# Here, we will use the StringIO method for setting
# as the file object.
file_1 = SIO(string_1)
# by using the following command, the user will be able to return is the file #interactive or not.
print ("Is the file stream above interactive?", file_1.isatty())
# by using the following command,
# the user will be able to return is the file readable or not.
print ("Is the file stream above readable?", file_1.readable())
# by using the following command,
# the user will be able to return does the file support writing or not.
print ("Is the file stream above writable?", file_1.writable())
# by using the following command, , the user will be able to return is the file #seekable or not.
print ("Is the file stream above seekable?", file_1.seekable())
# by using the following command, the user will be able to return is the file #closed or not.
print ("Is the file above closed?", file_1.closed)
输出:
Is the file stream above interactive? False
Is the file stream above readable? True
Is the file stream above writable True
Is the file stream above seekable? True
Is the file above closed? False
3. StringIO.seek(): seek()函数用于设置文件中光标的位置。如果我们对文档执行任何写或读操作,光标会被置于上次使用的索引位置,以便我们可以使用seek()函数从文件的开头位置移动光标。
语法:
以上方法的语法是:
File_name.seek(argument)
#This argument tells the function where to place the cursor.
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 ='Hello and thank you for visiting to Javatpoint.com.'
# Here, we will use the StringIO method for setting as the file object.
file_1 = SIO(string_1)
# here, the user will be able to Read the file:
print (file_1.read())
#If the user wishes to view the file again, it will display empty file since the #cursor has been set to the last index. It will also not print anything because #the function returns an empty string.
print (file_1.read())
# So, to set the cursor position for reading or writing the file again
# we can use seek() function.
#We can pass any index here form(0 to len(file))
file_1.seek(0)
# Now the user can read the file again
print (file_1.read())S
输出:
Hello and thank you for visiting to Javatpoint.com.
Hello and thank you for visiting to Javatpoint.com.
4. StringIO.truncate(): 此函数用于调整文件流的大小。该方法保存文件并在给定索引后删除文件。
语法:
以上方法的语法是:
File_name.truncate(size = None)
# The user can provide the size from where to truncate the file.
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 ='Hello and welcome to Javatpoint.com.'
# Here, we will use the StringIO method for setting as the file object.
file_1 = SIO(string_1)
# here, we can read the initial file:
print(file_1.read())
# for setting the cursor at 0.
file_1.seek(0)
# for dropping the file after the given index, i.e., 14.
file_1.truncate(14)
# here, it will print the File after truncate.
print(file_1.read())
输出:
Hello and welcome to Javatpoint.com.
Hello and welc
5. StringIO.tell(): 这个方法用于告知文件的当前流和光标位置。
语法:
以上方法的语法是:
File_name.tell()
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 ='Hello and welcome to Javatpoint.com.'
# Here, we will use the StringIO method for setting as the file object.
file_1 = SIO(string_1)
# Here the cursor is set at index '0'.
print(file_1.tell())
# now, we are setting the Cursor to index '23'.
file_1.seek(23)
# here, we will be printing the index of cursor
print(file_1.tell())
输出:
0
23
6. StringIO.close() 这是用于关闭文件的函数。此函数在文件上被调用,我们不能对其执行任何操作。进行的任何操作都会导致 ValueError 。
语法: =
以上方法的语法是:
File_name.close(
示例:
# First, we will import the StringIO module.
from io import StringIO as SIO
# The arbitrary string.
string_1 ='Hello and welcome to Javatpoint.com.'
# Here, we will use the StringIO method for setting as the file object.
file_1 = SIO(string_1)
# here, we can read the initial file:
print(file_1.read())
# for closing the current file.
file_1.close()
# If the user would perform any operation on the above file now, it will raise an #ValueError.
# here, we will be using the closed function to know whether the file is closed #or not.
print("Is the file closed?", file_1.closed)
输出:
Hello and welcome to Javatpoint.com.
Is the file closed? True