一、文件读写:
1、读模式:r不能打开不存在的文件,不能写
2、写模式:w打开不存在的文件会新建,清空文件,不能读
3、追加模式:a打开不存在的文件会新建,不能读
4、r+模式:打开不存在的文件会报错,能写
5、w+模式:打开不存在的文件,新建,清空原有内容,读不到内容
6、a+模式:打开不存在的文件,新建,清空原有内容,能读,需要seek到文件最开头的位置
7、flush()刷新缓冲区
*read
f.read()#读取所有内容
f.readline()#读取一行
f.readlines()#读取所有行,list格式
fw.writelines(a)#writelines可以写入list,write只能写入字符

1、高效读取文件:
readline()一行一行读,不会占用大量内存
f = open('user')
d={}
while 1:#有数据就是真
line = f.readline().strip()#一行一行读取
if line:
user,pwd=line.split(',')
d[user]=pwd
else:
break
print(d)
2、直接循环文件对象,每次取的都是文件里的每一行
for line in f:
print(line)
二、集合是无序
*交集:stus1.intersecton(stu2)
合并集合:stus1 | stus2或者stus1.union(stus2)
差集:(前面集合有的,后面集合没有的)stus1-stus2或者stus2.difference(stus1)
对称差集,只有一个集合里面出现过的,都输出:stus1.symmetric_difference(stus2)或者 stus1^stus2
增加元素:stus.add('xx')
stus.pop()随机删除
stus.remove('xx')指定元素删除
可以for循环:for s in stus:
print(s)
*random模块:
import random
s='dkfjskldjfksjld'
l=[1,2,3,4]
random.randint(1,23)随机取一个整数1-23之间的
random.choice(s)随机取一个元素
random.sample(s,3)随机选择3个元素
random.uniform(1,19)
round(f,3)取3位小数
random.shuffle(l)只能传list,洗牌,打乱数据
三、修改文件
import os
os.remove('xx')#删除原文件
os.rename('新文件名字','旧文件名字')将文件的名字改为旧文件的名字

s.replace('旧内容‘,‘新内容‘’’’)
f.seek()文件指针
f.truncate()清除文件内容

四、函数
def 函数名(参数):
xxx
