Getting started with Python(17)

Getting started with Python (17/18)
Section 17 **** Documents

Hello everyone! Today we are going to talk about an important Python operation object: files.

We are actually happening all the time about the interactive behavior in the program. For example, when you want to get the user's input and print out some returned results to the user, the input() and print() functions are used.

However, we sometimes encounter some information that needs to be persisted. In addition to using a database, are there any simple and flexible methods? The answer is yes. That is the python file we are going to talk about today.

**1、 Recap: input() and print() **

**Explanation: **In this example, we used the string slice operation to realize the text flip function. As mentioned earlier, we can slice the sequence from position a to position b using str[a:b:c]. c is the step of the slice (Step). The default step size is 1, it will return a continuous text. If a negative step is given, such as -1, the flipped text will be returned.

2、 File operation functions: open(), read(), readline(), write(), close()

(1) You can create (ie "open") an object belonging to the file file class through the open() function.

(2) You can read or write files through methods such as read(), readline(), write(), etc.

(3) When you finish using the file, you can call the close() method to close the operation object of the file.

PS: The ability to read or write files depends on how you specify to open the file.

File opening modes include: reading mode ('r'), writing mode ('w'), append mode ('a'), text reading mode ('t'), binary reading mode ('b'). You can view more other modes and details through help(open). By default, open() will open the file as text reading.

3、 Pickle

File operations are indeed very convenient, so, in addition to text content, other data types of objects, can we also store them in a file?

Yes, there is! It seems that as long as you have the needs, Python will prepare the solution for you.

**By importing the Pickle module, you can put any pure Python Object Storage into a file and retrieve it later. This is also called persistent storage of objects. **

**Package: **

First open the file in write (w) binary (b) mode through open, and then call the dump function of the pickle module. This process is called encapsulation (Pickling).

**Unpacking: **

Then, we receive the returned object through the load function of the pickle module. This process is called unpickling.

4、 Unicode

Have you noticed that the code we write has always been only in English or numbers. What should I do if I encounter non-English, such as Chinese?

Usually we will introduce unicode encoding format to solve the problem of Chinese recognition and display, such as the following code. Among them, class means class, that is, type. class'str', displayed as the type of string.

Strings that use unicode encoding all begin with the letter u, such as u "hello world", but after python3, it doesn't really need to be so troublesome. The default is unicode encoding.

5、 utf-8

When we read unknown files or write files with non-English characters, we need to specify Unicode characters as "UTF-8" encoding format in open(). It is recommended to set # encoding**=**** utf-8 **This comment is habitually placed at the top of the program.

summary

In this section of the course, we discussed various types of input and output, including temporary, and persistent, such as file handling, and the pickle module, as well as Unicode knowledge and so on.

If you are interested, I suggest you use python to independently develop your own "address book", how about?

Any questions can be discussed in the comments below. When you finish this project independently, you should be confident that you have become a real python programmer.

To become a good programmer, the most important way is to write more code! **Next, in our next episode of extended knowledge, we will also share more interesting features of python.

Recommended Posts

Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Getting started with Python (2)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting started with Numpy in Python
Getting started with Ubuntu
Getting Started with Python-4: Classes and Objects
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started python learning steps
How to get started quickly with Python
python requests.get with header
Play WeChat with Python
Web Scraping with Python
Implementing student management system with python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
How to make a globe with Python
Automatically generate data analysis report with Python
Create dynamic color QR code with Python
Python | Quickly test your Python code with Hypothesis