0%

Python相关知识

1、文件读写

1、打开文件

使用open()函数来进行文件的读写,调用close()来关闭文件

1
2
3
4
5
6
7
8
file = open(file, mode='r', buffering=-1, encoding=None,
errors=None, newline=None, closefd=True, opener=None)
# file 为要打开文件的文件的路径,相对路径或绝对路径都可以
# mode 为打开模式,默认为只读
# encoding 为编码格式

# 关闭文件
file.close()

打开模式:

字符 意义
'r' 读取(默认)
'w' 写入,并先截断文件
'x' 排它性创建,如果文件已存在则失败
'a' 写入,如果文件存在则在末尾追加
'b' 二进制模式
't' 文本模式(默认)
'+' 更新磁盘文件(读取并写入)

2、普通文件读写

1、文件读取
1
2
3
4
5
6
7
8
9
10
11
12
13
file = open('./Others/sample.txt')
# read() 会一次性读取文件中的所有内容,下次在读取时会返回为空
content = file.read()

# readline() 只会读取一行的内容
content = file.readline()

# readlines() 会读取所有内容储存在一个列表中,每行的内容作为一个元素
file.close()

# 使用for循环遍历文件的每一行(每一行会带着换行符)
for line in open('./Others/sample.txt'):
print(line, end='')
2、文件写入
1
2
3
4
5
6
7
file = open('./Others/sample.txt', 'w')
# write() 写入不会自动换行
file.write('98765\n')
file.write('43210\n')
# writelines()
file.writelines(['01234\n', '56789\n'])
file.close()

3、CSV文件读写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import csv


def csv_read():
csvfile = open('./Others/tmp.csv', 'r')
reader = csv.reader(csvfile)
for row in reader:
print(row)
csvfile.close()


def csv_write():
csvfile = open('./Others/tmp.csv', 'w')
writer = csv.writer(csvfile)
writer.writerow(['title', 'url'])
writer.writerow(['6', '7'])
writer.writerow(['8', '9'])
writer.writerow(['0', '0'])
csvfile.close()


if __name__ == "__main__":
csv_read()
csv_write()

4、Json文件读写

1
2
3
4
5
6
7
8
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
with open(cur_path + 'tmp.json', 'w') as file:
'''Json文件写入'''
json.dump(numbers, file)
with open(cur_path + 'tmp.json', 'r') as file:
'''Json文件读入'''
numbers = json.load(file)
print(numbers)

5、Excel文件读写

1、安装模块
1
pip3 install xlrd xlwt
2、Excel文件读取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def read():
# 打开excel
readbook = xlrd.open_workbook(cur_path+'tmp.xlsx')
# 读取文件sheet
sheet: xlrd.sheet.Sheet
sheet = readbook.sheet_by_index(2) # 通过索引,索引从0开始
sheet = readbook.sheet_by_name('Sheet1') # 通过名称,区分大小写
# 获取sheet的最大行数和列数
nrows = sheet.nrows
ncols = sheet.ncols
print(nrows, ncols)
# 获取某个单元格的值
value = sheet.cell(0, 0).value
print(value)
# 获取某一列
row = sheet.row(0)
# 获取某一行
col = sheet.col(0)
print(row)
print(col)
3、Excel文件写入
1
2
3
4
5
6
7
8
9
10
11
12
def write():
# 打开一个excel
writebook = xlwt.Workbook()
# 添加一个sheet
sheet: xlwt.Worksheet
sheet = writebook.add_sheet('Sheet1')
# 将数据写入单元格
value = 'test'
# 将数据写入第0行0列的单元格
sheet.write(0, 0, value)
# 保存
writebook.save(cur_path+'tmp_1.xlsx')

2、函数

1、传递任意数量的实参

1
2
3
4
5
6
7
8
9
10
11
# 概述要制作的披萨
def make_pizza(size, *toppings):
'''多个实参传进来会封装成一个元组'''
print("Making a %d-inch pizza with the following toppings:" % size)
for topping in toppings:
print('- ' + topping)


if __name__ == "__main__":
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

2、使用任意数量的关键字实参

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 创建用户信息字典
def build_profile(first, last, **user_info):
'''多个实参传进来会封装成一个元组'''
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile


if __name__ == "__main__":
user_profile = build_profile(
'albert', 'einstein', location='princeton', field='physics')
print(user_profile)

3、类

1、定义类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Myclass(object):
# 初始化方法,当类被实例化时会自动调用
def __init__(self, name=None):
self.name = 'null'
if name is not None:
self.name = name

def getName(self):
return self.name

def setName(self, name):
self.name = name


# 实例化
tmp = Myclass('Tom')
# 调用类的方法
tmp.setName('Jack')
tmp.getName()

2、继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Person(object):

def __init__(self, name):
self.name = name

def get_details(self):
return self.name


class Student(Person):

def __init__(self, name, branch, year):
Person.__init__(self, name)
self.branch = branch
self.year = year

# 重写父类中的方法
def get_details(self):
return "{} studies {} and is in {} year.".format(self.name, self.branch, self.year)


class Teacher(Person):

def __init__(self, name, papers):
Person.__init__(self, name)
self.papers = papers

# 重写父类中的方法
def get_details(self):
return "{} teachers {}.".format(self.name, ','.join(self.papers))


# Student 和 Teachers 均继承了 Person
per = Person('Sachin')
stu = Student('Kushal', 'CSE', 2005)
tea = Teacher('Prashad', ['C', 'C++'])
print(per.get_details())
print(stu.get_details())
print(tea.get_details())

3、多继承

1
2
3
4
5
6
class MyClass(Parentclass1, Parentclass2,...):
def __init__(self):
Parentclass1.__init__(self)
Parentclass2.__init__(self)
...
...

4、属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Person(object):
person_name = 'person name' # 类属性

def __init__(self):
self.name = 'name' # 实例属性
self._name = '_name' # 私有属性
self.__name = '__name'


per = Person()

# 类属性直接使用 '类名.属性' 调用,也可以使用 '实例化对象.属性' 调用
print(Person.person_name)
print(per.person_name)

# 私有属性,用一个且有一个前导的下划线来表明非公有的方法和实例变量
print(per.name)
print(per._name)

# 双前导的下划线应该仅仅用来避免与其子类属性的命名冲突
# print(per.__name)

5、方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Person(object):
country = 'China'

# 实例方法
def f1(self):
print('实例方法')

# 私有方法
def __f2(self):
print('私有方法')

'''
类方法是类所拥有的方法,需要用修饰器@classmethod来标识其为类方法。
类方法第一个参数必须是类对象,一般以cls作为第一个参数(就像self一样),也可以有别的参数。
'''
@classmethod
def get_country1(cls):
return cls.country

'''
静态方法需要通过修饰器 @staticmethod来进行修饰,静态方法不需要多定义参数。
'''
@staticmethod
def get_country2():
return Person.country


per = Person()

# 实例方法
per.f1()

# 私有方法无法从外部调用,同私有属性一样
# per.__f2()

# 类方法,同类属性一样
Person.get_country1()
per.get_country1()

# 静态方法
Person.get_country2()
per.get_country2()

5、装饰器

更精确的调整控制属性访问权限,可以使用 @property 装饰器,@property 装饰器就是负责把一个方法变成属性调用的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Account(object):

def __init__(self, rate):
self.__amt = 0
self.rate = rate

@property
def amount(self):
return self.__amt

@property
def cny(self):
return self.__amt * self.rate

@amount.setter
def amount(self, value):
if value < 0:
print("Sorry, no negative amount in the account.")
return
self.__amt = value


def main():
acc = Account(6.6)
acc.amount = 20
print('Dollar amount:', acc.amount)
print('In CNY:', acc.cny)
acc.amount = -100
print('Dollar amount:', acc.amount)


if __name__ == '__main__':
main()

4、模块和包

1、模块

Python有一种方法可以把定义放在一个文件里,并在脚本或解释器的交互式实例中使用它们。这样的文件被称作 模块 ;模块中的定义可以 导入 到其它模块或者 模块(你在顶级和计算器模式下执行的脚本中可以访问的变量集合)。

导入模块,即调用另一个.py文件里的内容(两个文件位于相同目录,或者被调用文件位于Python路径)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'''
test文件,进行调用测试
'''
import dividing


def main():
# 调用dividing中的函数
dividing.star_line(10)
dividing.hash_line(10)
dividing.simple_liem(10)


if __name__ == '__main__':
main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'''
dividing.py
定义了分割线的函数
'''
def star_line(num):
print('*' * num)


def hash_line(num):
print('#' * num)


def simple_liem(num):
print('-' * num)

扩展:模块的搜索路径

模块的搜索路径都储存在sys.path列表中

1
2
3
import sys
for path in sys.path:
print(path)

2、包

含有 __init__.py 文件的目录可以用来作为一个包,目录里的所有 .py 文件都是这个包的子模块。

如果 __init__.py 文件内有一个名为 __all__ 的列表,那么只有在列表内列出的名字将会被公开。

1
2
3
4
5
6
7
8
9
10
11
12
13
'''
tmp_1.py
调用mymodule测试
'''
import mymodule


def main():
mymodule.simple_liem(10)


if __name__ == '__main__':
main()
1
2
3
4
5
6
'''
mymodule/__init__.py
mymodule包的__init__.py文件
'''
from mymodule.dividing import simple_liem
__all__ = [simple_liem, ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'''
dividing.py
定义了分割线的函数
'''
def star_line(num):
print('*' * num)


def hash_line(num):
print('#' * num)


def simple_liem(num):
print('-' * num)

目录结构:需要注意的是tmp_1.pymymodule包同级别路径

模块和包-1

3、安装自定义包

1、自定义包

目录结构

1
2
3
4
5
6
7
|--MyModules
| |--setup.py
| |--src
| | |--mm
| | | |--downloader.py
| | | |--tools.py
| | | |--__init__.py

mm为自定义包,其中包含自定义模块tools.py

src/mm/__init__.py中导入模块,使用绝对引入

1
2
3
4
5
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from . import tools

__all__ = [tools]
2、设置自定义包的参数

setup.py文件中设置包的相关参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-

from distutils.core import setup
from setuptools import find_packages

setup(name='mm', # 包名
version='1.1.0', # 版本号
description='My common constants and functions', # 描述
author='Shifa Yang', # 作者
author_email='ysf504703038@163.com', # 作者邮箱
url='null', # 包的网址
license='',
# install_requires=['requests', 'selenium', 'bs4'], # 安装依赖
classifiers=[
'Intended Audience :: Developers',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
],
keywords='',
packages=find_packages('src'), # 必填
package_dir={'': 'src'}, # 必填
include_package_data=True,
)
3、安装

在命令行执行打包

1
python setup.py sdist

就会生成一个dist目录,目录中有自定义包的安装包,到dist目录下使用pip安装

1
2
3
4
# 本地安装
pip3 install packages
# 或者从git安装
pip install git+https://github.com/504703038/My-Python-Modules.git

5、压缩与解压

1、gz文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import gzip


# 压缩
def compress(filename):

file = open(filename, 'rb')
content = file.read()
file.close()
filename = filename+'.gz'
gz_file = gzip.GzipFile(filename, 'wb')
gz_file.write(content)
gz_file.close()


# 解压
def decompress(filename):
# 加载.gz文件
gz_file = gzip.GzipFile(filename)
# 读取文件内容
content = gz_file.read()
# 关闭文件
gz_file.close()
# 解压后的文件名
filename = filename.replace('.gz', '')
# 创建解压后的文件
file = open(filename, 'wb+')
# 输出压缩文件的内容
file.write(content)
# 关闭文件
file.close()


if __name__ == "__main__":
compress('./Others/sitemap_201103.xml')
decompress('./Others/sitemap_201103.xml.gz')

6、目录访问

1
2
3
4
5
6
7
8
9
10
11
12
import os


def file_name(file_dir):
for root, dirs, files in os.walk(file_dir):
print('root_dir:', root) # 当前目录路径
print('sub_dirs:', dirs) # 当前路径下所有子目录
print('files:', files) # 当前路径下所有非目录子文件


if __name__ == "__main__":
file_name('./Others')

7、字符串编码转换

1
2
3
4
5
6
7
s = '编码'
s = s.encode('utf-8')
s = s.encode('gbk')
# 转换后变为字节流
s = b'\x08'
s = s.decode('utf-8')
# 转换后变为 str

8、print输出

1、保留小数

1
2
3
4
5
a = 0.123456789
b = 0.987654321
# 保留5为小数,b保留3位小数
print('a = {:.5f}, b = {:.3f}'.format(a, b))
print('a = %.5f,b = %.3f' % (a,b))

输出结果:

1
a = 0.12346, b = 0.988

可以看出,保留小数的时候会四舍五入,如果不想四舍五入

1
2
3
4
5
6
7
import math
a = 0.123456789
b = 0.987654321
# 保留5为小数,b保留3位小数
a = math.floor(a * 10 ** 5) / 10 ** 5
b = math.floor(b * 10 ** 3) / 10 ** 3
print(a, b)

9、字符串处理

方法 描述
string.capitalize() 把字符串的第一个字符大写
string.center(width) 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
string.count(str, beg=0, end=len(string)) 返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
string.find(str, beg=0, end=len(string)) 检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
string.rfind(str, beg=0,end=len(string) ) 类似于 find()函数,不过是从右边开始查找.

10、打印异常信息

1
2
3
4
5
6
7
8
import traceback
try:
2/0
except Exception as e:
print(e) # 直接打印异常
print('遇到错误:%s' %e) # 将异常转换为字符串打印(只有异常原因,没有回溯信息)
traceback.print_exc() # 同 print(e)
traceback.format_exc() # 将异常转换为字符串但不打印(包含回溯信息)