📜  Python中的StringIO模块

📅  最后修改于: 2022-05-13 01:54:29.307000             🧑  作者: Mango

Python中的StringIO模块

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())


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())


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)


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 form(0 to len(file))
file.seek(0)
 
# Now we can able to read the file again()
print(file.read())


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())


Python3
# Importing the StringIO module.
from io import StringIO 
 
 
# The arbitrary string.
string ='Hello and welcome to GeeksForGeeks.'
 
# Using the StringIO method to
# set aas 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())


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)


输出:

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.closed:如果文件关闭,此函数返回 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 form(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 aas 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