Stringio Python
很多时候,数据读写不一定是文件,也可以在内存中读写。 StringIO顾名思义就是在内存中读写str。 要把str写入StringIO,我们需要先创建一个StringIO,然后,像文件一样写入即可:.
Strings are amongst the most popular types in Python. We can create them simply by enclosing characters in quotes. Python treats single quotes the same as double quotes. Creating strings is as simple as assigning a value to a variable. For example −var1 = 'Hello World!'
Var2 = 'Python Programming'Accessing Values in StringsPython does not support a character type; these are treated as strings of length one, thus also considered a substring.To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. Basketball stars unblocked. For example −.
#!/usr/bin/python3var1 = 'Hello World!' Print ('Updated String:- ', var1:6 + 'Python')When the above code is executed, it produces the following result −Updated String:- Hello PythonEscape CharactersFollowing table is a list of escape or non-printable characters that can be represented with backslash notation.An escape character gets interpreted; in a single quoted as well as double quoted strings. #!/usr/bin/python3print ('My name is%s and weight is%d kg!'
#!/usr/bin/python3parastr = 'this is a long string that is made up ofseveral lines and non-printable characters such asTAB ( t ) and they will show up that way when displayed.NEWLINEs within the string, whether explicitly given likethis within the brackets n , or just a NEWLINE withinthe variable assignment will also show up.' 'print (parastr)When the above code is executed, it produces the following result. Note how every single special character has been converted to its printed form, right down to the last NEWLINE at the end of the string between the 'up.' And closing triple quotes. Also note that NEWLINEs occur either with an explicit carriage return at the end of a line or its escape code (n) −this is a long string that is made up ofseveral lines and non-printable characters such asTAB ( ) and they will show up that way when displayed.NEWLINEs within the string, whether explicitly given likethis within the brackets , or just a NEWLINE withinthe variable assignment will also show up.Raw strings do not treat the backslash as a special character at all. Every character you put into a raw string stays the way you wrote it −.
#!/usr/bin/python3print (r'C:nowhere')When the above code is executed, it produces the following result −C:nowhereUnicode StringIn Python 3, all strings are represented in Unicode.In Python 2 are stored internally as 8-bit ASCII, hence it is required to attach 'u' to make it Unicode. Culdcept revolt cards. It is no longer necessary now. Built-in String MethodsPython includes the following built-in methods to manipulate strings − Sr.No.Methods & Description1Capitalizes first letter of string2Returns a string padded with fillchar with the original string centered to a total of width columns.3Counts how many times str occurs in string or in a substring of string if starting index beg and ending index end are given.4Decodes the string using the codec registered for encoding.
Example: Using the StringIOmodule to capture output# File:import StringIOimport string, sysstdout = sys.stdoutsys.stdout = file = StringIO.StringIOprint 'According to Gbaya folktales, trickery and guileare the best ways to defeat the python, king ofsnakes, which was hatched from a dragon at theworld's start. National Geographic, May 1997'sys.stdout = stdoutprint string.upper(file.getvalue)$ python stringio-example-3.pyACCORDING TO GBAYA FOLKTALES, TRICKERY AND GUILEARE THE BEST WAYS TO DEFEAT THE PYTHON, KING OFSNAKES, WHICH WAS HATCHED FROM A DRAGON AT THEWORLD'S START. NATIONAL GEOGRAPHIC, MAY 1997It’s often good idea to wrap the code that generates output in atry-finally statement, and restore the original sys.stdout in the finallyclause. In this way, stdout will be restored even if the output operationfails.