StringIO 模块是一个内存中的类似文件的对象。该对象可用作大多数需要标准文件对象的函数的输入或输出。创建 StringIO 对象时,通过将字符串传递给构造函数来对其进行初始化。如果没有传递任何字符串,StringIO 将以空开始。在这两种情况下,文件上的初始光标都从零开始。
注意:最新版本的 Python 中不存在此模块,因此要使用此模块,我们必须从 Python 中的 io 模块将其导入为 io.StringIO。
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='This is initial string.'
# Using the StringIO method to set
# as file object. Now we have an
# object file that we will able to
# treat just like a file.
file = StringIO(string)
# this will read the file
print(file.read())
# We can also write this file.
file.write(" Welcome to geeksforgeeks.")
# This will make the cursor at
# index 0.
file.seek(0)
# This will print the file after
# writing in the initial string.
print('The string after writing is:', file.read())
输出:
This is initial string.
The string after writing is: This is initial string. Welcome to geeksforgeeks.
StringIO的一些重要方法如下:
1. StringIO.getvalue():该函数返回文件的全部内容。
用法:
File_name.getvalue()
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method to
# set as file object.
file = StringIO(string)
# Retrieve the entire content of the file.
print(file.getvalue())
输出:
'Hello and welcome to GeeksForGeeks.'
2. 在本文中,我们讨论 StringIO 的一些返回布尔值的函数,即 True 或 false:
- StringIO.isatty():如果流是交互式的,则此函数返回 True;如果流不是交互式的,则返回 False
- StringIO.readable():如果文件可读,则该函数返回 True;如果文件不可读,则返回 False。
- StringIO.writable():如果文件支持写入,则该函数返回 True;如果文件不支持写入,则返回 False。
- StringIO.seekable():如果文件支持随机访问,则此函数返回 True;如果文件不支持随机访问,则返回 False。
- StringIO.关闭:如果文件关闭则此函数返回 True,如果文件打开则返回 False。
用法:
File_name.isatty() File_name.readable() File_name.writable() File_name.seekable() File_name.closed
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method to
# set as file object.
file = StringIO(string)
# This will returns whether the file
# is interactive or not.
print("Is the file stream interactive?", file.isatty())
# This will returns whether the file is
# readable or not.
print("Is the file stream readable?", file.readable())
# This will returns whether the file supports
# writing or not.
print("Is the file stream writable?", file.writable())
# This will returns whether the file is
# seekable or not.
print("Is the file stream seekable?", file.seekable())
# This will returns whether the file is
# closed or not.
print("Is the file closed?", file.closed)
输出:
Is the file stream interactive? False Is the file stream readable? True Is the file stream writable? True Is the file stream seekable? True Is the file closed? False
3.StringIO.seek():seek()函数用于设置光标在文件上的位置。如果我们对文件执行任何读写操作,光标将设置在最后一个索引上,以便将光标移动到文件的起始索引处seek()。
用法:
File_name.seek(argument) # Here the argument is passed to tell the # function where to set the cursor position.
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method
# to set as file object.
file = StringIO(string)
# Reading the file:
print(file.read())
# Now if we again want to read
# the file it shows empty file
# because the cursor is set to
# the last index.
# This does not print anything
# because the function returns an
# empty string.
print(file.read())
# Hence to set the cursor position
# to read or write the file again
# we use seek().We can pass any index
# here from(0 to len(file))
file.seek(0)
# Now we can able to read the file again()
print(file.read())
输出:
Hello and welcome to GeeksForGeeks. Hello and welcome to GeeksForGeeks.
4.StringIO.truncate():该函数用于调整文件流的大小。此方法在提供的索引之后删除文件并保存它。
用法:
File_name.truncate(size = None) # We can provide the size from where # to truncate the file.
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method
# to set as file object.
file = StringIO(string)
# Reading the initial file:
print(file.read())
# To set the cursor at 0.
file.seek(0)
# This will drop the file after
# index 18.
file.truncate(18)
# File after truncate.
print(file.read())
输出:
Hello and welcome to GeeksForGeeks. Hello and welcome
5.StringIO.tell():该方法用于告知文件的当前流或光标位置。
用法:
File_name.tell()
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method to
# set as file object.
file = StringIO(string)
# Here the cursor is at index 0.
print(file.tell())
# Cursor is set to index 20.
file.seek(20)
# Print the index of cursor
print(file.tell())
输出:
0 20
6.StringIO.close():该方法用于关闭文件。在文件上调用此函数后,我们无法对该文件执行任何操作。如果执行任何操作,都会引发 ValueError。
用法:
File_name.close()
例子:
Python3
# Importing the StringIO module.
from io import StringIO
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
# Using the StringIO method to
# set as file object.
file = StringIO(string)
# Reading the file.
print(file.read())
# Closing the file.
file.close()
# If we now perform any operation on the file
# it will raise an ValueError.
# This is to know whether the
# file is closed or not.
print("Is the file closed?", file.closed)
输出:
Hello and welcome to GeeksForGeeks. Is the file closed? True
相关用法
- Python String format()用法及代码示例
- Python String capitalize()用法及代码示例
- Python String center()用法及代码示例
- Python String casefold()用法及代码示例
- Python String count()用法及代码示例
- Python String endswith()用法及代码示例
- Python String expandtabs()用法及代码示例
- Python String encode()用法及代码示例
- Python String find()用法及代码示例
- Python String index()用法及代码示例
- Python String isalnum()用法及代码示例
- Python String isalpha()用法及代码示例
- Python String isdecimal()用法及代码示例
- Python String isdigit()用法及代码示例
- Python String isidentifier()用法及代码示例
- Python String islower()用法及代码示例
- Python String isnumeric()用法及代码示例
- Python String isprintable()用法及代码示例
- Python String isspace()用法及代码示例
- Python String istitle()用法及代码示例
- Python String isupper()用法及代码示例
- Python String join()用法及代码示例
- Python String ljust()用法及代码示例
- Python String rjust()用法及代码示例
- Python String lower()用法及代码示例
注:本文由纯净天空筛选整理自VishwashVishwakarma大神的英文原创作品 StringIO Module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。