當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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

輸出:

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


相關用法


注:本文由純淨天空篩選整理自VishwashVishwakarma大神的英文原創作品 StringIO Module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。