The path of Python (ii)

Source: Internet
Author: User

(1) Python's built-in function (BIF)

More than 70 bif are built into the python3, and several commonly used are:

List (): Creates a new, empty list.

Range (): Enter the number of times parameter to return an iterator with a fixed number of iterations.

Enumerate (): Create a "numbered list" of data, starting with 0.

Int (): Tells a string or another number to be converted to an integer.

ID (): Returns a unique identifier for a Python data object.

Next (): Returns the next item in a data structure (such as a list) that can be iterated.

(2) file operation:

1. Open File:

Obj_file = open ("demo.txt","w")

Normally open file mode is "R" (read in the way), if you need to write the content, then use the mode "W". The open function returns a file object.

2. Write to File:

Print (" content to be written to the file ", File=obj_file) """ The file parameter is used to specify which object to write to (and if not, the function of the print function is to print the string) """

3. Close the file:

Obj_file.close () "" " Open file Be sure to close it! """

(3) Use the finally extension try:

At some point, Python's exception handling can corrupt your data, such as when working with files, if you write the "Close files" code into a try, and once the close function is in front of the problem, the try block throws an exception directly, skipping the code to close the file. This will cause your files to not be closed and your data may be corrupted. The workaround is to use the finally extension try to write the code that closes the file out of the try, such as:

Try: Demo_file= Open ('Demo.txt','W')    Print("Demo Word", file=demo_file)exceptIOError:Print('failed to write to file')finally: Demo_file.close ()

In doing so, the code in the finally will be executed, no matter what happens.

(4) With statement:

With the WITH statement you don't have to shut down the file when you close it, it automatically closes a file that might be open. Such as:

Try : With    open ('demo.txt',"w") as data:         Print('word_data', file=data)except  IOError as err:    print(' write file failed ')

(5) Isinstance function:

It is used to check whether a particular identifier contains data of a particular type, such as:

Demo_list = [123,456]isinstance (demo_list,list) "" "checks for the presence of a list type of data in Demo_list, which is, of course," true "  "" "= 1isinstance (demo_int,list) " " this time of course is false " ""

(6) Pickle Library

The role of the Pickle Library is to translate data into a "common format" that is stored on disk, placed in a database, or transmitted over a network to another computer. And when I need to use this data, I can turn it over and turn it into "the original format."

Use pickle: Import the Pickle module first, use Dump () to save the data, and later use load () to recover the data. (Note: This data must be opened using "Binary access Mode"), as in the following example:

ImportPickle"""Import Pickle Module"""    ··· List= [A.'Demo']"""This is the data we need to store."""With Open ('Demo.txt','WB') as Demo_file:""""B" in WB means binary open"""pickle.dump (list,demo_file)"""dump data into a demo.txt file"""    ···"""When we need to use the data stored above"""With Open ('Demo.txt','RB') as Demo_file:a_list= Pickle.load (Demo_file)"""Bring up the files in the Demo.txt"""Print(a_list)"""The printed content is the same as the list."""

About the syntax of Pickle.dump ():

Pickle.dump (object, file, [Usage Protocol])

Data to be persisted "object", saved to "file", the use of the Protocol has 3, index 0 is ascii,1 is old-fashioned 2, 2 is the new 2-binary protocol, the difference is more efficient in the latter.

The path of Python (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.