[Python from zero to one] 3. Grammar-based file operations, CSV file reading and writing, and object-oriented

Welcome everyone to "Python from zero to one", here I will share about 200 Python series articles, take everyone to learn and play together, and see the interesting world of Python. All articles will be explained in combination with the case, code and the author's experience. I really want to share my nearly ten years of programming experience with you. I hope it will be helpful to you. Please also Haihan for the deficiencies in the article.

The overall framework of the Python series includes 10 basic grammar, 30 web crawlers, 10 visual analysis, 20 machine learning, 20 big data analysis, 30 image recognition, 40 artificial intelligence, 20 Python security, and 10 other skills . Your attention, likes and reposts are your greatest support for Xiuzhang. Knowledge is priceless and people are affectionate. I hope we can all be happy and grow together on the road of life.

This article refers to the author's CSDN article, the link is as follows:

At the same time, the author's newly opened "Nazhang AI Security Home" will focus on Python and security technology, mainly sharing Web penetration, system security, artificial intelligence, big data analysis, image recognition, malicious code detection, CVE reproduction, threat intelligence Analysis and other articles. Although the author is a technical novice, he will ensure that every article will be written with great care. I hope these basic articles will be helpful to you and make progress with everyone on the road to Python and safety.



1. File operation

File refers to a collection of data stored on external media. The encoding methods of text files include ASCII format, Unicode code, UTF-8 code, GBK code, etc. The operation flow of the file is a trilogy of "open file-read and write file-close file".

1. Open the file

To open a file, call the open() function, and the return result is a file object. The function prototype is as follows:

<variable> = open(<name>, <mode>)
    -<name>表示打开文件名称
    -<mode>表示文件打开模式

The common parameters of mode include:

  • r: Read only, the file pointer will be placed at the beginning of the file
  • w: Write only, if the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted; if the file does not exist, create a new file
  • a: Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file; if the file does not exist, create a new file for writing
  • rb: read-only binary file, generally used for non-text files such as pictures
  • wb: write only binary files, generally used for non-text files such as pictures
  • ab: Open a file in binary format for appending
  • w+: Open a file for reading and writing

The complete syntax of the open() function is as follows:

  • open(file, mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Insert picture description here

Give a simple example:

infile = open("test.txt","r")

Note: Use the open() method to ensure that the file object is closed, that is, call the close() method.


2. Read and write files

(1) Reading files
Commonly used file reading methods include:

  • The return value of read() is a string containing the entire text content
  • readline() returns a string containing the content of the next line of the file content
  • The return value of readlines() is a list of the entire file content, each item in the list is a line of string

Examples are as follows:

infile = open("test.txt","r",encoding="utf8")
data = infile.read()
print(data)
print("")

infile = open("test.txt","r",encoding="utf8")
list_data = infile.readlines()
print(list_data)

The output result is shown below:

Insert picture description here
Insert picture description here


(2) Write file
To write data from computer memory to file, the methods include:

  • write() write a string containing text data or binary data set to the file
  • writelines() accepts a string list parameter for the list operation and writes it to the file
outfile1 = open('test.txt','a+',encoding="utf8")
str1 = '\nhello\n'  
str2 = 'world\n'
outfile1.write(str1)  
outfile1.write(str2)
outfile1.close()
  
outfile2 = open('test02.txt','w',encoding="utf8")
outfile2.writelines(['hello',' ','world'])  
outfile2.close()

infile = open('test.txt','r',encoding="utf8")
data = infile.read()
print(data)

Complete the additional write operation for the test.txt file, complete the new and write operations for the test02.txt file, and call the write() and writelines() methods to write data at the same time.

Insert picture description here

Insert picture description here


3. Close the file

After the file is read and written, remember to use the close() method to close the file. If you forget to use the close statement, when the program suddenly crashes, the program will not continue to perform the write operation, even after the program has completed the file write operation normally, because the file is not closed, the file may not contain the written The data. For safety, you need to close the file after using it. It is recommended that readers use try-except-finally exception capture statements and close the file in the finally clause.

try:
	#文件操作
except :
    #异常处理
finally:
	file.close()

Other methods include:

  • file.flush(): flush the internal buffer of the file
  • file.next(): return the next line of the file
  • file.seek(offset[, whence]): Set the current position of the file
  • file.tell(): Returns the current position of the file
  • file.truncate([size]): intercept the file, the intercepted bytes are specified by size

4. Loop through the files

In data crawling or data analysis, file traversal is often involved. The for loop is usually used to traverse the file content. On the one hand, you can call the read() function to read the file loop output, and on the other hand, you can also call the readlines() function. The comparison code of the two methods is as follows:

infile = open('test02.txt', 'r', encoding="utf8")
for line in infile.readlines():
   print(line)
print(infile.close())

infile = open('test02.txt', 'r', encoding="utf8").read()
for line in infile:
   print(line)
print(infile.close())

The output result "Jing Ye Si" is shown in the figure below, including TXT file and output value.

Insert picture description here



2. CSV file operation

When we use Python for web crawling or data analysis, we usually encounter CSV files, which are similar to Excel tables. Then we add the basic knowledge of SCV file reading and writing.

CSV (Comma-Separated Values) is a commonly used storage file, with a comma separator, and a semicolon between values. Import the CSV extension package in Python to use, including writing files and reading files.

1. CSV file writing

The basic process is as follows:

  • Import CSV module
  • Create a CSV file object
  • Write to CSV file
  • Close file
# -*- coding: utf-8 -*-
import csv
c = open("test-01.csv", "w", encoding="utf8")  #写文件
writer = csv.writer(c)
writer.writerow(['序号','姓名','年龄'])
 
tlist = []
tlist.append("1")
tlist.append("小明")
tlist.append("10")
writer.writerow(tlist)
print(tlist,type(tlist))
 
del tlist[:]  #清空
tlist.append("2")
tlist.append("小红")
tlist.append("9")
writer.writerow(tlist)
print(tlist,type(tlist))
 
c.close()

The output result is shown below:

Insert picture description here

Note that there will be extra blank lines at this time, we need to perform simple processing, add the parameter "newline=''" to solve.

  • c = open(“test-01.csv”, “w”, encoding=“utf8”, newline=’’)

Insert picture description here


2. CSV file reading

The basic process is as follows:

  • Import CSV module
  • Create a CSV file object
  • Read CSV file
  • Close file
# -*- coding: utf-8 -*-
import csv
c = open("test-01.csv", "r", encoding="utf8")  #读文件
reader = csv.reader(c)
for line in reader:
    print(line[0],line[1],line[2])
c.close()

The output result is shown below:

Insert picture description here

Encoding problems in file operations are the most troublesome, especially in Python2. However, it only needs to be consistent with the environment code, pay attention to the relevant conversion can also be effectively resolved, and the Python3 file read and write operations can be displayed normally by writing the encoding clearly. If it is a database, web page, and background language, the three encoding methods need to be consistent, such as utf8 or gbk, etc., and specific problems can be solved in detail! The follow-up author will explain the operation of the CSV file in conjunction with the crawler.

Insert picture description here



3. Object-oriented foundation

The traditional programming method is process-oriented and executes from top to bottom according to business logic. Object-oriented programming is another programming method. This programming method needs to be implemented using "classes" and "objects", and functions are encapsulated. A way of programming closer to real life.

Object-oriented is an object that regards objective things as attributes and behaviors, forms classes by abstracting the common properties and behaviors of objects of the same class, and realizes code reuse through class inheritance and polymorphism. An object is an instance of a class. If you compare an object to a house, then the class is the design drawing of the house, and the properties and methods are defined in the class.

The three basic characteristics of object-oriented are:

  • Encapsulation : Encapsulate objective things into abstract classes, where the data and methods in the class allow the class or object to operate.
  • Inheritance : After the subclass inherits the parent class, it can use all the functions of the parent class without rewriting the original class and can extend the functions.
  • Polymorphism : The attributes or behaviors defined in a class can have different data types or exhibit different behaviors after being inherited by a special class. Each class can exhibit different semantics. The two methods of implementation are overriding and overloading.

In Python, a class is a template. The template can contain multiple functions, which implement some functions; an object is an instance created based on the template, and the function in the class can be executed through the instance object. as follows:

#创建类
class 类名:
	#创建类中的函数,self特殊参数,不能省略
	def 函数名(self):
		#函数实现
		
#根据类创建对象obj
obj = 类名()

Suppose you need to write a program to calculate the area and perimeter of a rectangle. The idea is to define the length and width of two variables, and then define the method of calculating the area and perimeter in the class and use it as an example. code show as below:

#-*- coding:utf-8 -*-
class Rect:
    def __init__(self, length, width):
        self.length = length;
        self.width = width;

    def detail(self):
        print(self.length, self.width)

    def showArea(self):
        area = self.length * self.width
        return area

    def showCir(self):
        cir = (self.length + self.width) * 2
        return cir
#实例化
rect1 = Rect(4,5)
#调用函数
rect1.detail()
area = rect1.showArea()
cir = rect1.showCir()
print('面积:', area)
print('周长:', cir)

The output area is 20 and the perimeter is 18. For object-oriented encapsulation, it is actually encapsulating the content into the object using the construction method, and then obtaining the encapsulated content directly or indirectly through the object.

Insert picture description here

On the whole, object-oriented is to think and solve problems from the perspective of things themselves. If the above is implemented in the form of process-oriented definition functions, when there are multiple shapes, you need to define a method for each shape. Object-oriented only needs to abstract the attributes and methods of these shapes to form various shapes, which are more in line with the real situation.

Note: In order to allow readers to learn Python data crawling, data analysis, image recognition and other knowledge more concisely and quickly, the codes in this series are rarely presented in the way of defining classes and objects, but directly based on the functions or cases that need to be implemented. Directly write the corresponding code or function implementation. This is irregular and unreasonable. In actual development or more standardized code, it is recommended that you use an object-oriented approach to programming, but this series wants to show you the principle through the most concise code, and then you can improve and practice your skills.



Another example of object-oriented design pattern : In order to facilitate children's learning to program, X company has developed a Racing Car simulator. Using this simulator, each child can use a simple language to control a car, such as right, left, etc. Please design a simple language, give its grammar and class diagram of the language.

This is the problem related to life in our actual programming. It involves knowledge of design patterns. The method I use is implemented by "naming mode". The client defines Children and Car, and the requester is Right and Left sent by Children. , Up, Down commands, the receiver is Car executes Move(), the abstract command is the abstract interface of up, down, left and right, and the specific command is the up, down, left, and right of Car. The class diagram I made is as follows:

Insert picture description here
In this example, we used object-oriented thinking, thinking and solving problems from the perspective of things themselves, rather than the formal implementation of process-oriented functions. If there is another air-land vehicle, it can not only move up and down, left and right, but also fly. The traditional method also needs to write four more methods for moving up and down, and the object-oriented directly inherits Car and adds a new flying method. (Without direction), this is the benefit of object-oriented.

Similarly, through this example, I am not trying to prove whether the command mode or the drawn class diagram is correct. What I want to explain is that our learning object-oriented knowledge is mainly used to solve problems in real life, so that it can solve problems more efficiently. And optimized code. At the same time, object-oriented thinking must adapt to changes in requirements and solve the actual needs of users. Changes must be considered as much as possible in the design, which involves abstraction, encapsulation changes (emphasis), design patterns and other knowledge.



Four. Summary

In any case, the author hopes that this article can give you some knowledge of Python, and hope that you can follow me to write code and make progress together. If the article can provide some trivial ideas and help to your research or project, it will be even more gratifying. The author’s greatest expectation is that some of the stories, words, codes, or cases in the article will be helpful to you and to those who work hard.

Appreciation of the previous article:

Finally, sincerely thank you for your attention to the "Nazhang House" public account, thank CSDN for so many years of company, will always insist on sharing, hope that my article can accompany you to grow, and hope to continue to advance on the road of technology. If the article is helpful and sentimental to you, it is the best return to me, read it and cherish it! The official account established on August 18, 2020, thank you again for your attention, and please help to promote the "Nazhang House", haha~ newcomer, please give me your advice.

Insert picture description here

Unfortunately, let us know how to be perfect.
Passing away, let us keep moving forward.
Her figure in the night is so beautiful.

(By: Nazhang House Eastmount 2020-09-22 night in Wuhan https://blog.csdn.net/Eastmount )


The references are as follows:

Guess you like

Origin blog.csdn.net/Eastmount/article/details/108737755