logo

Python の StringIO モジュール

それは 文字列IO module はメモリ内のファイルのようなオブジェクトです。これは、ユーザーが通常のファイル オブジェクトから期待できる機能の大部分を入力または出力するために使用できます。ユーザーが StringIO オブジェクトを作成すると、最初はコンストラクターに文字列を提供することによって作成されます。文字列がない場合、StringIO は空になります。どちらの場合も、ファイル上に最初に表示されるカーソルは 0 から始まります。

このモジュールは Python の最新バージョンでは使用できません。したがって、このモジュールを使用できるようにするには、io.StringIO の形式で Python の Io モジュールに転送する必要があります。

例:

mysqlで列の型を変更する
 # 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. ここでは、ブール値、つまり false または true を返す StringIO の関数のいくつかを見ていきます。

    isatty():StringIO のこの関数は、ストリームがインタラクティブでない場合は False を返し、ストリームがインタラクティブである場合は True を返すために使用されます。読み取り可能():StringIO のこの関数は、ファイルが読み取れない場合は False を返し、ファイルが読み取れる場合は True を返すために使用されます。書き込み可能():StringIO のこの関数は、ファイルが書き込みをサポートしていない場合は False を返し、ファイルが書き込みをサポートしている場合は True を返すために使用されます。シーク可能():StringIO のこの関数は、ファイルがランダム アクセスをサポートしていない場合は False を返し、ファイルがランダム アクセスをサポートしている場合は True を返すために使用されます。閉まっている: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() が使用されます。

構文:

上記のメソッドの構文は次のとおりです。

 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(): この関数は、ファイル ストリームのサイズを変更するために使用されます。このメソッドはファイルを保存し、指定されたインデックスの後に削除します。

構文:

上記のメソッドの構文は次のとおりです。

int を 2 倍にする
 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() これはファイルを閉じるために使用されます。この関数はファイルに対して呼び出されますが、そのファイルに対して操作を実行することはできません。実行される操作の結果は次のとおりです。 値エラー

構文: =

上記のメソッドの構文は次のとおりです。

 File_name.close( 

例:

Linuxコマンドのディレクトリ
 # 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