day11-总结

2018-07-26  本文已影响117人  RurouniKenshin

模块管理函数和类

1.什么是模块

一个.py文件就是一个模块
other

name = 10
print(name)

def func1():
    print('====')

other2

name2 = 100
print(name2)

def func2():
    print('~~~~~~~~')

if __name__ == '__main__':
    pass

other3

def func3():
    print('++++++')


if __name__ == '__main__':
    name3 = 100
    print(name3)

2.import:可以通过import关键字导入其他的模块

import 模块名(.py文件名)

直接导入模块的时候,相当于把被导入模块里面的内容粘贴到import的位置

3.怎么使用模块内的内容?什么内容是可以直接使用的?

模块名. 的方式去使用模块中的内容
模块中声明的全局变量都可以使用(普通变量,函数,类)

import other

print(other.name, other.func1())

4. from 模块名 import 内容1,内容2,内容3...... --> 导入模块中指定的内容

通过这种方式在使用内容的时候,不用在被导入的变量或函数或类前加模块名

from other2 import func2

func2()

5.阻止被导入的模块中不必要的代码被粘贴到当前模块

一般情况下,除了函数的声明和类的声明以外,其他的代码都放到这个if里面
_name_:是每个模块自带的一个属性,用来保存当前这个模块的名字
但是当正在执行当前模块时,这个属性名字是固定的_main_

if __name__ == '__main__':
    pass

此中代码不会被其他模块使用

import other3

other3.func3()

import 模块名 as 新名字
from 模块名 import 函数名 as 新的函数名

# 练习,使用一个模块,用来管理所有和形状相关的功能
# 求两点间的距离,求圆周长,圆面积,矩形周长和面积
import gh_opration

dis = gh_opration.distance([1, 2], [4, 6])
print(dis)
c_zc = gh_opration.circle_girth(4)
c_mj = gh_opration.circle_area(4)
print(c_zc, c_mj)
r_zc = gh_opration.rectangle_girth(3, 4)
r_mj = gh_opration.rectangle_area(3, 4)
print(r_zc, r_mj)

gh_opration的函数

def distance(x, y):
    import math
    return math.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)


def circle_girth(r):
    return 2 * 3.14 * r


def circle_area(r):
    return 3.14 * r ** 2


def rectangle_girth(length, width):
    return 2 * (length + width)


def rectangle_area(length, width):
    return length * width

文件操作

(所有)文件操作流程:打开文件 -> 操作文件(读/写) -> 关闭文件

1.打开文件

open(文件路径,打开的方式,编码方式)
文件路径(必填) --> 决定打开哪个文件
打开方法(默认值是'r') --> 决定打开文件后是进行什么样的操作
'r' - 读操作(读出来是字符串)
'rb'/'br' - 读操作(读出来的数据是二进制)
'w' - 写操作(可以将文本数据写入文件中)
'wb'/'bw' - 写操作(可以将二进制数据写入文件中)
'a' - 写操作(追加)

编码方式 --> 主要针对文本文件的读写
(不同的操作系统默认的文本编码方式不同,windows-->gbk,mac-->utf-8)

2.文本文件读操作

a.放在工程外的文件,写绝对路径

     open('E:/python-1805/test.txt')

(推荐)
b.将文件放到工程目录下的某个位置,写相对路径(相对于工程目录):./相对路径 或者../相对路径

    # open('./test1.txt')
    # open('./files/test2.txt')

    # - 打开文件,返回文件句柄(文件代言人)

    f = open('./test1.txt','r',encoding='utf-8')

    # - 读文件(获取文件的内容)
    '''
    文件.read():获取文件内容,并且返回
    read(n) --> n 设置读的长度
    '''
    content = f.read()
    print(content)
    # - 关闭文件
    f.close()

3.文本文件写操作

    # - 打开文件
    f2 = open('./files/test2.txt','w',encoding='utf-8')
    # - 写文件
    f2.write('ajsgd氨基酸的38r5')
    # - 关闭文件
    f2.close()

注意:以读的方式打开文件,文件不存在,会报错FileNotFindError
以写的方式打开文件,文件不存在,会自动创建这个文件

4.二进制文件的读写操作(音频,视频,图片文件都是二进制文件)

    f3 = open('./files/timg.jpg','rb')
    # - 读文件
    image_data = f3.read()
    # bytes:python中二进制数据对应的数据类型
    print(type(image_data),image_data)
    # 关闭文件
    f3.close()

    f4 = open('./files/new.jpg','wb')
    # - 写操作
    f4.write(image_data)
    # 关闭文件
    f4.close()

5.通过with关键字打开文件

with open() as 文件变量名:
文件操作
在文件操作结束后会自动去关闭文件

python与json

json文件(文本),就是文件后缀是.json的文件。内容必须是json格式的内容
json格式:
1.内容是字符串
2.最外层是字典,字典里面必须是键值对
3最外层是数组(列表),里面必须是数组(列表)

import json

1.json文件的读操作

    with open('./files/myinfo.json','r',encoding='utf-8') as o_j:
        context = json.load(o_j)
        print(context)
    print(context['age'])

load(文件对象):获取指定json文件中的内容,返回值的类型是json文件最外层的对应的数据类型

2.json文件的写操作

    with open('./files/myinfo2.json','w',encoding='utf-8') as o_j2:
        w_context = [{'name':'a1'},{'name':'b1'}]
        # dump(写的内容,文件对象)
        json.dump(w_context,o_j2)
import json
name = input('name:')
tel = input('tel:')
stu = {"name":name,"tel":tel}
try:
    with open('./files/student.json','r',encoding='utf-8') as o_j3:
        students = json.load(o_j3)
except FileNotFoundError:
    students = []
with open('./files/student.json','w',encoding='utf-8') as o_j3:
    students.append(stu)
    json.dump(students,o_j3)
上一篇 下一篇

猜你喜欢

热点阅读