logo

StringIO-module in Python

Het is de StringIO module is een in-memory, bestandsachtig object. Het kan worden gebruikt voor het invoeren of uitvoeren van de meeste functies die gebruikers van een gewoon bestandsobject kunnen verwachten. Zodra de gebruiker de StringIO-objecten heeft gemaakt, wordt deze in eerste instantie gemaakt door een string aan de constructor op te geven. Als er geen string is, is de StringIO leeg. In beide gevallen begint de aanvankelijk weergegeven cursor op het bestand bij nul.

De module is niet beschikbaar in de meest recente versie van Python; Om deze module te kunnen gebruiken, moeten we deze dus overbrengen naar de Io-module in Python in de vorm van io.StringIO.

Voorbeeld:

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

Uitgang:

 This is the initialized string. The file of the string after writing in it is: This is the initialized string. Welcome to Javatpoint.com. 

Belangrijke methoden van StringIO:

Hieronder volgen enkele methoden van StringIO:

1. StringIO.getwaarde(): Deze functie wordt gebruikt voor het retourneren van de volledige inhoud van het bestand.

Syntaxis:

Syntaxis van de bovenstaande methode is:

 File_name.getvalue() 

Voorbeeld:

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

Uitgang:

 Hello and thank you for visiting to Javatpoint.com 

2. Hierin kijken we naar enkele functies van StringIO die een Booleaanse waarde retourneren, d.w.z. false of true:

    isatty():Deze functie van StringIO wordt gebruikt om False te retourneren als de stream niet interactief is en True als de stream interactief is.leesbaar():Deze functie van StringIO wordt gebruikt om False te retourneren als het bestand niet leesbaar is en True als het bestand wel leesbaar is.beschrijfbaar():Deze functie van StringIO wordt gebruikt om False te retourneren als het bestand schrijven niet ondersteunt, en True als het bestand schrijven ondersteunt.zoekbaar():Deze functie van StringIO wordt gebruikt om False te retourneren als het bestand geen willekeurige toegang ondersteunt, en True als het bestand willekeurige toegang ondersteunt.gesloten:Deze functie van StringIO wordt gebruikt voor het retourneren van False als het bestand open is en retourneert True als het bestand gesloten is.

Syntaxis:

Syntaxis van de bovenstaande methode is:

 1. File_name.isatty() 2. File_name.readable() 3. File_name.writable() 4. File_name.seekable() 5. File_name.closed 

Voorbeeld:

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

Uitgang:

 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(): De zoeken() functie wordt gebruikt om de positie van de cursor binnen het bestand in te stellen. Als we een schrijf- of leesbewerking op een document uitvoeren, wordt de cursor op de index geplaatst die het laatst is gebruikt, zodat we de cursor vanaf de beginpositie van het bestand kunnen verplaatsen. seek() wordt gebruikt.

Syntaxis:

Syntaxis van de bovenstaande methode is:

 File_name.seek(argument) #This argument tells the function where to place the cursor. 

Voorbeeld:

 # 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 

Uitgang:

 Hello and thank you for visiting to Javatpoint.com. Hello and thank you for visiting to Javatpoint.com. 

4. StringIO.truncate(): Deze functie wordt gebruikt om de grootte van de bestandsstream te wijzigen. Met deze methode wordt het bestand opgeslagen en na de opgegeven index neergezet.

lijsten in Java

Syntaxis:

Syntaxis van de bovenstaande methode is:

 File_name.truncate(size = None) # The user can provide the size from where to truncate the file. 

Voorbeeld:

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

Uitgang:

 Hello and welcome to Javatpoint.com. Hello and welc 

5. StringIO.tell(): Deze methode wordt gebruikt om de huidige stream en cursorpositie van het bestand te bepalen.

Syntaxis:

Syntaxis van de bovenstaande methode is:

 File_name.tell() 

Voorbeeld:

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

Uitgang:

 0 23 

6. StringIO.close() Dit wordt gebruikt voor het sluiten van het bestand. Deze functie wordt aangeroepen voor een bestand en we kunnen er geen bewerkingen op uitvoeren. Elke bewerking die wordt uitgevoerd, resulteert in een Waardefout .

Syntaxis: =

Syntaxis van de bovenstaande methode is:

 File_name.close( 

Voorbeeld:

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

Uitgang:

 Hello and welcome to Javatpoint.com. Is the file closed? True