Python技能树

版权声明:本文为博主原创文章,未经许可不得转载 https://blog.csdn.net/linxid/article/details/83269918

1. 重要模块:

1.1 os.path( )模块总结:

http://www.runoob.com/python3/python3-os-path.html
https://www.cnblogs.com/renpingsheng/p/7065565.html
https://www.cnblogs.com/wuxie1989/p/5623435.html

1.2 logging模块总结:

1.3 argparse模块总结:

三步走战略:

  1. 创建 ArgumentParser() 对象
  2. 调用 add_argument() 方法添加参数
  3. 使用 parse_args() 解析添加的参数

示例:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('integer', type=int, help='display an integer')
args = parser.parse_args()

print args.integer

可选参数:
也就是在命令行中不一定要写的参数,调用的时候是可选的。参数名前面加--来实现。

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("--square", help="display a square of a given number", type=int)
parser.add_argument("--cubic", help="display a cubic of a given number", type=int)

args = parser.parse_args()

$ python argparse_usage.py --square 8
64

$ python argparse_usage.py --cubic 8
512

``
Python 超好用標準函式庫 argparse
argparse 使用

1.4 json详解

http://www.runoob.com/python/python-json.html

1.5 copy详解

深copycopy.deepcopy()新建一个对象重新分配内存地址,复制对象内容。浅copycopy.copy()不重新分配内存地址,内容指向之前的内存地址。浅copy如果对象中有引用其他的对象,如果对这个子对象进行修改,子对象的内容就会发生更改。

import copy

#这里有子对象
numbers=['1','2','3',['4','5']]
#浅copy
num1=copy.copy(numbers)
#深copy
num2=copy.deepcopy(numbers)

#直接对对象内容进行修改
num1.append('6')

#这里可以看到内容地址发生了偏移,增加了偏移‘6’的地址
print('numbers:',numbers)
print('numbers memory address:',id(numbers))
print('numbers[3] memory address',id(numbers[3]))
print('num1:',num1)
print('num1 memory address:',id(num1))
print('num1[3] memory address',id(num1[3]))


num1[3].append('6')

print('numbers:',numbers)
print('num1:',num1)
print('num2',num2)

输出:
numbers: ['1', '2', '3', ['4', '5']]
numbers memory address: 1556526434888
numbers memory address 1556526434952
num1: ['1', '2', '3', ['4', '5'], '6']
num1 memory address: 1556526454728
num1[3] memory address 1556526434952
numbers: ['1', '2', '3', ['4', '5', '6']]
num1: ['1', '2', '3', ['4', '5', '6'], '6']
num2 ['1', '2', '3', ['4', '5']]

1.6 requests:

官方快速上手

2. 基础语法提升

2.1 小技巧:

2.1.1 replace:

str.replace(old, new[, max])
	old -- 将被替换的子字符串。
	new -- 新字符串,用于替换old子字符串。
	max -- 可选字符串, 替换不超过 max
#!/usr/bin/python3

str = "www.w3cschool.cc"
print ("菜鸟教程旧地址:", str)
print ("菜鸟教程新地址:", str.replace("w3cschool.cc", "runoob.com"))

str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))

2.2 assert

assert断言语句的语法格式:

assert condition
# 等价于
if condition:
	raise AssertionError()

conditionTrue的时候,相当于这句话不被执行,当conditionFalse的时候,则会提示错误。举例如下:

assert 2>1    # 无输出
assert 2<1
# 输出为:
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-d5ddf35f108d> in <module>()
----> 1 assert 2<1

AssertionError: 

assert断言语句添加异常参数:

assert的异常参数,在断言表达式后添加字符串信息,用来解释哪里出了问题。
格式如下:

assert expression, arguments
assert 表达式, 参数

举例如下:

>>> assert len(lists) >=5,'the lenght of list shorter than 5'
Traceback (most recent call last):
File "D:/Data/Python/helloworld/helloworld.py", line 1, in <module>
assert 2>=5,'the lenght of list shorter than 5'
AssertionError: the lenght of list shorter than 5

>>> assert 2==1,'2 is not equal 1'
Traceback (most recent call last):
File "D:/Data/Python/helloworld/helloworld.py", line 1, in <module>
assert 2==1,'2 is not equal 1'
AssertionError: 2 is not equal 1

2.3 格式化字符串:

python3字符串format最佳实践
python3 f-string格式化字符串的高级用法
(那些過時的) Python 字串格式化以及 f-string 字串格式化

3.Ubuntu命令:

# 获取解压后文件不能读取的权限
sudo chmod u+rw train.csv

猜你喜欢

转载自blog.csdn.net/linxid/article/details/83269918