list comprehension
[ expression for variable in range(n) if condition] is equivalent to
for 变量 in in range(n):
if condition:
expression
Pros: Easy to write, Cons: Hard to read
Note: Square brackets are used, if they are parentheses, it is not a list comprehension, but a generator (not required)
Lambda anonymous function
lambda variable: the expression expression is the processing logic of the function, the return value before the colon
For example, to achieve the factorial of 5 1*2*3*4*5=120
Advantages: easy to write, Disadvantages: not easy to read, can only write simple functions, can only be used once
os module
os is the operating system module
1. getcwd() gets the current directory:
2. chomd() to modify file permissions: only for linux, not for windows 1 execute, 2 write, 4 read
3. chdir() changes the current path:
4. mkdir() to create a new folder: you can use an absolute path or a relative path
5. rmdir() delete folders: only empty folders can be deleted
6. makedirs() creates a folder recursively: if there is no test1, it will also create a new test1
7. removedirs() deletes the folder recursively: it will delete both test1 and test3
8. listdir() gets the files under the folder: returns a list (does not display files in subfolders)
9. rename() rename:
10. stat() to get file attributes:
11. remove() deletes the file:
12. sep to get the path separator:
13. linesep to get the line separator:
14. pathsep get the environment variable separator:
15. environ gets environment variables:
16. name gets the system name: windows is nt, linux is posix
17. system() executes the operating system command: there will be garbled characters, and it cannot be a dynamic command: if it is top, it should be changed to top -n 1
18. popen() executes operating system commands: there will be no garbled characters
19. path.abspath() gets the absolute path of the file:
20. path.split() separates folders and files:
21. path.join() merges paths:
22. path.dirname() gets the parent folder:
23. path.basename() gets the last folder:
24. path.exists() determines whether the path exists:
25. path.isabs() determines whether it is an absolute path:
26. path.isfile() determines whether it is a file:
27. path.isdir() determines whether it is a folder:
28. path.isdir() determines the size of the file (folder): bytes
29. path.walk() gets all files and folders in a folder:
show only path
show only folders
only show files
sys module
1.platform to get the platform name: windows is win32, linux is linux2
2 .path to get python's environment variables:
3 .argv gets the parameters of the interpreter: Note that the first parameter is the file name
time module
Time formats include timestamp, string, and tuple. Timestamps and strings cannot be converted directly, you need to pass a tuple: timestamp<=>tuple<=>string
1. time() gets the current timestamp: the timestamp is a floating point number
2 .strftime() gets the current time string: You can also convert the tuple to a string
3. gmtime() gets the standard time tuple:
4. localtime() gets the local time tuple: East Eighth District = standard time zone + 8 hours, you can also convert the timestamp to a tuple
5. mktime() tuple to timestamp:
6. strptime() string to tuple:
datetime module
Compared with the time module, the datetime module has the advantage that it is convenient to calculate the time difference
1. datetime.today() gets the current time: the format is datetime.datetime
2. date.today() gets the current date: the format is datetime.date
3. datetime.timedelta() gets the time difference:
4. strftime() converts datetime to string:
5. datetime.fromtimestamp() converts timestamp to datetime:
pymysql module
Used to connect to the mysql database
1. Connection library: pymysql.connect()
2. Create a cursor: connect.cursor()
3. Execute sql: cursor.execute() Note that select does not require commit, other sql requires commit
4. Get the result: cursor.fetchall()
5. Close the cursor: cursor.close()
6. Close the connection: connection.close ()
xlwt module
Used to write to excel, only in xls format, xlsx cannot use this module
1. Open the workbook: Workbook()
2. Create a new worksheet: workbook.add_sheet ()
3. Write the header and content: worksheet.wirte () (row number, column number, content), note that the rows and columns are counted from 0
4. Save: workbook.save ()
hashlib module
for encryption
1 .md5() : md5 digest algorithm (sha256 and other digest algorithms, the same usage)
2 .update() : Enter a byte string. Note that if it is str, it must be converted to byte first
str转byte: ’’.encode()=b’’
byte转str: b’’.decode()=’’
3.hexdigest():获取摘要,格式仍为str