logo

StringIO-modul i Python

Det er StringIO modul er et fil-lignende objekt i hukommelsen. Den kan bruges til at indtaste eller udlæse de fleste funktioner, som brugere kan forvente af et almindeligt filobjekt. Når brugeren har oprettet StringIO-objekterne, oprettes de i første omgang ved at give en streng til konstruktøren. Hvis der ikke er nogen streng, vil StringIO være tom. I begge tilfælde vil den oprindeligt viste markør på filen starte ved nul.

Modulet er ikke tilgængeligt i den seneste version af Python; for at kunne bruge dette modul, skal vi overføre det til Io-modulet i Python i form af io.StringIO.

Eksempel:

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

Produktion:

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

Vigtige metoder til StringIO:

Følgende er nogle metoder til StringIO:

1. StringIO.getvalue(): Denne funktion bruges til at returnere hele indholdet af filen.

Syntaks:

Syntaksen for ovenstående metode er:

 File_name.getvalue() 

Eksempel:

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

Produktion:

 Hello and thank you for visiting to Javatpoint.com 

2. I dette ser vi på nogle af funktionerne i StringIO, der returnerer en boolsk værdi, dvs. enten falsk eller sand:

    isatty():Denne funktion af StringIO bruges til at returnere False, hvis strømmen ikke er interaktiv, og True, hvis strømmen er interaktiv.læselig():Denne funktion af StringIO bruges til at returnere False, hvis filen ikke er læsbar, og True, hvis filen er læsbar.skrivbar():Denne funktion i StringIO bruges til at returnere False, hvis filen ikke understøtter skrivning, og True, hvis filen understøtter skrivning.søgbar():Denne funktion af StringIO bruges til at returnere False, hvis filen ikke understøtter vilkårlig adgang, og True, hvis filen understøtter vilkårlig adgang.lukket:Denne funktion af StringIO bruges til at returnere False, hvis filen er åben, og den returnerer True, hvis filen lukkes.

Syntaks:

hyldehunde

Syntakser for ovenstående metode er:

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

Eksempel:

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

Produktion:

 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(): Det søge() funktionen bruges til at indstille markørens position i filen. Hvis vi udfører en skrive- eller læseoperation på et dokument, placeres markøren på det indeks, der sidst blev brugt, så vi kan flytte markøren fra startpositionen af ​​filen seek() er brugt.

Syntaks:

Syntaksen for ovenstående metode er:

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

Eksempel:

 # 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 

Produktion:

hvor mange nul for en million
 Hello and thank you for visiting to Javatpoint.com. Hello and thank you for visiting to Javatpoint.com. 

4. StringIO.truncate(): Denne funktion bruges til at ændre størrelsen på filstrømmen. Denne metode gemmer filen og dropper den efter det givne indeks.

Syntaks:

Syntakser for ovenstående metode er:

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

Eksempel:

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

Produktion:

 Hello and welcome to Javatpoint.com. Hello and welc 

5. StringIO.tell(): Denne metode bruges til at fortælle filens aktuelle strøm og markørposition.

Syntaks:

Syntakser for ovenstående metode er:

 File_name.tell() 

Eksempel:

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

Produktion:

 0 23 

6. StringIO.close() Dette bruges til at lukke filen. Denne funktion kaldes på en fil, og vi kan ikke udføre nogen handlinger på den. Enhver operation, der udføres, vil resultere i en ValueError .

Syntaks: =

Syntakser for ovenstående metode er:

 File_name.close( 

Eksempel:

junit test cases
 # 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) 

Produktion:

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