# ------------------*************正则表达式**********--------------
# match 用于指定文本模式和待匹配的字符串,起始要匹配对
# import re
# m = re.match("qyc","qyc")
# if m is not None:
# print(m.group())
# print(m.__class__.__name__)
# print(m)
#
# n = re.match("qyc","qychy") #起始必须匹配,否则为None
# if n is not None:
# print(n.group())
# print(n.__class__.__name__)
# print(n)
# search 搜索文本内包含指定字符
#匹配多个字符串 |
# import re
# n = "qyc|hy"
# m = re.search(n,"hyqyyqyc") #从0开始计数,匹配一个,结尾截至
# if m is not None:
# print(m.group())
# print(m)
# <_sre.SRE_Match object; span=(5, 8), match='qyc'>
# <_sre.SRE_Match object; span=(0, 2), match='hy'>
# . 匹配如何单个字符,.占一个字符
# import re
# m = "qy."
# n = re.search(m,"asdasqychy")
# if n is not None:
# print(n.group())
# print(n)
#使用字符集
# [ab] 表示或 a|b
#[ab][cd] ac ad bc bd
# m = "[ab]"
# import re
# n = re.match(m,"bcb")
# if n is not None:
# print(n)
# <_sre.SRE_Match object; span=(0, 1), match='b'>
# m = "[ab][cd]"
# import re
# n = re.match(m,"bcb")
# if n is not None:
# print(n)
# <_sre.SRE_Match object; span=(0, 2), match='bc'>
# 重复,可选和特殊字符 * 0-n + 1-n
# import re
# s = "a*"
# a = ["","a","aa","aaa","baa"]
# for i in a:
# m = re.match(s,i)
# if m is not None:
# print(m)
# <_sre.SRE_Match object; span=(0, 0), match=''>
# <_sre.SRE_Match object; span=(0, 1), match='a'>
# <_sre.SRE_Match object; span=(0, 2), match='aa'>
# <_sre.SRE_Match object; span=(0, 3), match='aaa'>
# <_sre.SRE_Match object; span=(0, 0), match=''> a*可以匹配空串
# import re
# s = "a+"
# a = ["","a","aa","aaa","baa"]
# for i in a:
# m = re.match(s,i)
# if m is not None:
# print(m)
# <_sre.SRE_Match object; span=(0, 1), match='a'>
# <_sre.SRE_Match object; span=(0, 2), match='aa'>
# <_sre.SRE_Match object; span=(0, 3), match='aaa'>
# |w 任意一个字符或数字 |d 任意一个数字 ?可有可无
# import re
# m = "(\w)*abc(d?)(\d?)+"
# n = re.search(m,"qabcd11")
# if n is not None:
# print(n)
# {3}代表三个
# a = "a{3}"
# import re
# m = re.search(a,"bhaa")
# if m is not None:
# print(m)
# 匹配一个邮箱
# email = "[a-zA-Z0-9]+@(\w+\.)*\w+\.com"
# import re
# m = re.search(email,"1227694865@qq.com")
# if m is not None:
# print(m)
# 分组
# import re
# m = re.match("(\d{3})-(\d{2})","123-2266")
# if m is not None:
# print(m)
# print(m.group(1))
# print(m.group(2))
# 匹配字符串 开始^ 结束 $ \b表示单词边界
# findall finditer 查找每一次出现的位置
# import re
# a = "11-abc-22-hy-333-qyc-444"
# m = re.findall(r"\d{2}-[a-z]{3}",a,re.I) #设置大小写不敏感
# if m is not None:
# print(m)
# for i in m:
# print(i)
# finditer 返回迭代器
# import re
# a = "11-abc-22-hy-333-qyc-444"
# m = re.finditer(r"\d{2}-[a-z]{3}",a,re.I) #设置大小写不敏感
# if m is not None:
# print(m)
# for i in m:
# print(i)
# print(i.group())
# <callable_iterator object at 0x007C65B0>
# <_sre.SRE_Match object; span=(0, 6), match='11-abc'>
# <_sre.SRE_Match object; span=(14, 20), match='33-qyc'>
# sub 和 subn 搜索与替换
# sub 返回替换后的结果 模式字符串 要替换的字符串 被替换的字符串
# import re
# a = "11-abc-22-hy-333-qyc-444"
# m = re.sub(r"\d{2}-[a-z]{3}","被替换",a)
# if m is not None:
# print(m)
# 被替换-22-hy-3被替换-444
# subn 返回一个元组,替换后的结果和总数
# import re
# a = "11-abc-22-hy-333-qyc-444"
# m = re.subn(r"\d{2}-[a-z]{3}","被替换",a)
# if m is not None:
# print(m)
# ('被替换-22-hy-3被替换-444', 2)
# split分隔字符串
# import re
# a = re.split("、","强月城、黄燕、java、Python")
# print(a)
# ['强月城', '黄燕', 'java', 'Python']
#一些常用的正则表达式
# Email "[0-9a-zA-Z]+@(\w\.)*\w+\.[a-z]{3}"
# import re
# a = "1227694865@qq.com"
# b = re.match("[0-9a-zA-Z]+@(\w\.)*\w+\.[a-z]{3}",a)
# print(b.group())
# IP地址 IPV4 "\d{1,3}"
# import re
# a = "192.168.32.128"
# b = re.match("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",a)
# print(b.group())
# 练习
# a = ["bat","Bit","But","hAt","hit","hut"]
# import re
# for i in a:
# b = re.match("[bh][aiu]t$",i,re.I)
# if b is not None:
# print(b)
# <_sre.SRE_Match object; span=(0, 3), match='bat'>
# <_sre.SRE_Match object; span=(0, 3), match='Bit'>
# <_sre.SRE_Match object; span=(0, 3), match='But'>
# <_sre.SRE_Match object; span=(0, 3), match='hAt'>
# <_sre.SRE_Match object; span=(0, 3), match='hit'>
# <_sre.SRE_Match object; span=(0, 3), match='hut'>
# import re
# a = "1111 2222 3333 4444"
# b = re.match("\d{4} \d{4} \d{4} \d{4}$",a)
# if b is not None:
# print(b)
# a = "2020-10-06"
# import re
# b = re.search("\d{4}-\d\d?-\d\d?$",a)
# print(b)
# ----------------**********常用模块**********---------
# sys 模块 定义了一些函数和变量 用来设置和获取系统信息
import sys
# print(sys.platform) 当前平台标识符 mac os为darwin windows为win32
# print(sys.argv) ['D:/自编程序/studyPython/lean2.py'] 命令行名称,argv[1]代表第一个命令行参数
# print(sys.modules) 已经装载的模块列表
# print(sys.stdout) <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> 标准输出流
# print(sys.stdin) <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> 标准输入流
# print(sys.stderr) <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> 标准错误流
# os 包括子模块path 包含了大量获取各种系统信息,以及对系统进行设置的函数
import os
# print("当前工作目录:"+os.getcwd()) 当前工作目录:D:\自编程序\studyPython
# print(os.listdir(os.getcwd())) 当前工作目录下的文件:['.idea', 'lean1.py', 'lean2.py']
# 改变当前工作目录
# os.chdir("C:") ../返回上级
# print()
# j = 0;
# for i in os.listdir(os.getcwd()):
# print(i)
# j=j+1;
# print(j)
# 文件与目录操作
# os.chdir("H:\py")
# if not os.path.exists("hy"): os.path.exists校验方法
# os.mkdir("hy")
# os.mkdir("hy") 第二个参数 0o777表示wrx权限
# mkdir 当目录存在时:FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'hy'
# os.chdir("H:\py")
# os.makedirs("a/b/c") 创建多级目录 第二个参数为权限 第三个参数为false 目录存在会抛出异常
# os.rmdir("hy")
# os.removedirs("a/b/c") 会abc全删掉
# os.remove("hy.txt")
# os.rename("hy.txt","hy") 改改名字
# os.renames("1/2/3","a/b/c")
# 软链接与硬链接 快捷方式/文件的拷贝 文件修改后,另一方也会改变
# os.chdir("H:\py")
# print(os.listdir(os.getcwd()))
# os.symlink("qyc.txt","qycr.txt") #要有管理员权限 管理员运行cmd 然后运行py程序
# os.link("qcc.txt","qccy.txt")
# 杂项
# print(os.sep) sep 返回平台路径分隔符 \
# print(os.pathsep) 返回环境变量之间的分隔符 ;
# print(os.name) nt 返回当前os名称
# print(os.environ) 以字典形式返回所有环境变量的值
# print(os.getenv("Path")) 获取指定环境变量的值
# os.putenv()设置环境变量
# os.system("dir") 执行命令
# 集合
# a = set([1,2,3])
# b = set((3,4,5))
# c = a.union(b) 合并
# d = a|b 合并
# e = a&b 交集
# print(e)
# a = set([1,2,4])
# b = set([1,2,3])
# print(a.issubset(b)) True a是否为b的子集
# print(b.issuperset(a)) # 超集
# print(b-a) 计算集合差
# print(b.difference(a))
# print(a^b) {3, 4} 对称差,相当于a-b|b-a
# in 属于
#堆
# from heapq import *
# from random import *
# data = [6,7,2,9,7,6,7,8,1]
# heap = [] #堆就是一个列表,只是heapq中的函数利用堆算法来改变列表中的元素而已
# for n in data:
# val = choice(data) #在列表中随机选取
# print(val,end=" ")
# heappush(heap,val) #heappush 将value加入堆
# print(heap)
#
# print(heappop(heap))
# print(heap)
# # heapify(data) #直接把data转换为堆
# # print(data) #1, 2, 6, 7, 7, 6, 7, 9, 8
#
# print(heapreplace(heap, 100)) #弹出最小值并插入一个值
# print(heap)
#
# print(nlargest(1, heap)) #得到最大的一个值
# print(nlargest(2, heap)) #得到最大的俩个值
#
# print(nsmallest(1, heap)) #得到最小值
# print(nsmallest(2, heap))
#
# print(list(merge([1, 3, 5], [2, 4, 6],[]))) #合并有序集合
# 双端队列
# 双端队列头尾都能进行操作
# from collections import deque
# q = deque(range(10)) #创建一个包含10个数字的双端队列
# print(q) #deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# q.append(100) #将100追加到q的队尾
# print(q)
# q.appendleft(-1) #在队首加-1
# print(q)
# print(q.pop()) #弹出队尾
# print(q.popleft()) #弹出队首
# print(q)
# # q.rotate(-2) #向左循环
# print(q)
# # deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# # deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
# q.rotate(4) #向右循环
# print(q)
# # deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# # deque([6, 7, 8, 9, 0, 1, 2, 3, 4, 5])
#
# # 创建双端队列
# q1 = deque([111,222,333])
# q.extend(q1)
# print(q)
# q.extendleft(q1) #q1会倒序
# print(q)
# 时间日期time
# import time
# ticks = time.time()
# print(ticks) #1603118556.3775516 到1971年1月1日午夜之间的秒数
# localtime = time.localtime(time.time())
# print(localtime) #time.struct_time(tm_year=2020, tm_mon=10, tm_mday=19, tm_hour=22, tm_min=46, tm_sec=47, tm_wday=0,一周的第几日 tm_yday=293,一年的第几日 tm_isdst=0 夏令时)
# # 获取可读时间
# print(time.asctime(time.localtime(time.time()))) #Mon Oct 19 22:52:46 2020
# 格式化日期和时间 strftime(格式化字符串,时间元祖)
# %y 俩位数年份 m月 d日 H24时 l12小时 M分钟数 S秒
# a简化星期名称 A完整星期名称 b简化月份名称 B完整月份名称
# c本地日期和时间 j一年中第几天 pAM PM U一年中的星期数,星期日为一个星期的开始 w星期 W一年中的星期数 星期1为开始
# x本地日期 X本地时间 Z当前时区名称 %%百分号本身
# import time
# import locale
# ----------------报错了 locale.setlocale(locale.LC_ALL,'zh_CN.UTF-8') #设置为utf-8 格式化字符串支持中文
# print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))
# print(time.strftime("a %a--%A b %b--%B c %c j %j p %p U %U w %w W %W x %x X %X Z %Z",time.localtime(time.time())))
# a Mon--Monday b Oct--October c Mon Oct 19 23:15:32 2020 j 293 p PM U 42 w 1 W 42 x 10/19/20 X 23:15:32 Z ?D1¨²¡À¨º¡Á?¨º¡À??
# 时间戳的增量
# import time
# time1 = time.time()
# time2 = time1+60 #加1分钟 60秒 60*60 一小时 60*60*24一天
# print(time1) 1603120709.662133
# print(time2) 1603120769.662133
# 计算日期和时间的插值
# import datetime
# import time
# d1 = datetime.datetime(2017,4,12)
# d2 = datetime.datetime(2018,12,25)
# print((d2-d1).days) #.seconds 求秒数
# d3 = datetime.datetime.now()
# print(d3) #2020-10-19 23:24:22.045103
# d4 = d3 + datetime.timedelta(hours=10) #往后延迟10小时
# print(d4) #2020-10-20 09:25:39.396251
# print(time.localtime(d4.timestamp())) #转换为时间元祖
# 获取某月某年的日历
# import calendar
# import locale
# cal = calendar.month(2020,10)
# print(cal)
# October 2020
# Mo Tu We Th Fr Sa Su
# 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
# locale.setlocale(locale.LC_ALL,"zh_CN.UTF-8") #中文
# cal1 = calendar.month(2020,10)
# print(cal1)
# 十月 2020
# 周一 周二 周三 周四 周五 周六 周日
# 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
# print(calendar.calendar(2020))
# 2020
#
# January February March
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22
# 27 28 29 30 31 24 25 26 27 28 29 23 24 25 26 27 28 29
# 30 31
#
# April May June
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
# 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
# 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
# 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
# 27 28 29 30 25 26 27 28 29 30 31 29 30
#
# July August September
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 5 1 2 1 2 3 4 5 6
# 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
# 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
# 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
# 27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
# 31
#
# October November December
# Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
# 1 2 3 4 1 1 2 3 4 5 6
# 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
# 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
# 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
# 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
# 30
# random 随机数
# randint(m,n) 包括m n
# random()0-1 包括0不包括1
# uniform(m,n) 阐述m-n的随机浮点数 包括m,n
# randrange(m,n,step) 在一个递增的序列中随机选择一个整数,step为步长, 1,6,2 【1,3,5】
#choice(seq) 从seq指定的序列中随机选择一个元素值,seq指定的列表元素可以是任意类型的值
#sample(seq,k) 随机选择k个元素
# shuffle(seq) 从seq指定的序列中元素的顺序打乱,该函数直接修改原有序列
# import random
# print(random.randint(1,10))
# print(random.random())
# print(random.randrange(1,3,1)) #不包括3
# print(random.uniform(1,1.5))
# list = [1,2,3,4,5,"a","b","c","d"]
# print(random.choice(list)) #随机选择一个元素
# print(random.sample(list,2)) #随机选择俩个
# random.shuffle(list)
# print(list) #[1, 3, 'b', 5, 'a', 'd', 2, 4, 'c']
#math
# import math
# print("圆周率:",math.pi)
# print("自然常熟:",math.e)
# print("绝对值:",math.fabs(-10))
# print("向上取整:",math.ceil(1.4)," ",math.ceil(1.8))
# print("向下取整:",math.floor(1.4)," ",math.floor(1.8))
# print("次方:",math.pow(2,4))
# print("平方根:",math.sqrt(9)," ",math.sqrt(8))
# print("正弦:",math.sin(math.pi/2))
# print("余弦:",math.cos(math.pi))
# print("正切:",math.tan(math.pi/4))
# 圆周率: 3.141592653589793
# 自然常熟: 2.718281828459045
# 绝对值: 10.0
# 向下取整: 2 2
# 向上取整: 1 1
# 次方: 16.0
# 平方根: 3.0 2.8284271247461903
# 正弦: 1.0
# 余弦: -1.0
# 正切: 0.9999999999999999
# ----------------***********文件和流****************------------------
# a 追加 w写 默认读 x排他的写模式 a追加模式 b二进制模式 t文本模式 +读写模式,必须与其他文件模式一起使用
# r+ 如果文件不存在抛异常 w+ 文件可读写 清空文件 a+ 若文件不存在创建新文件夹,存在文件指针在末尾
# try:
# f = open("H:/qyc.txt", "a+")
# f.seek(0)
# print(f.read())
# print(f.write("ssg"))
# finally:
# f.close()
# -----writelines readline readlines
# import os
# f = open("H:/qyc.txt", "w+")
# a = ["第1行"+os.linesep,"第2行"+os.linesep,"第3行"+os.linesep]
# a = ["第1行"+"\n","第2行"+"\n","第3行"+"\n"]
# f.writelines(a)
# f.close()
# f = open("H:/qyc.txt", "r")
# print(f.read())
# f.seek(0)
# print(f.readlines())
# --------***********使用fileInput
# import fileinput
# file = fileinput.input("H:/qyc.txt")
# print(type(file)) #<class 'fileinput.FileInput'>
# # 输出第一行 rstrip() 取出后面的空白字符
# print(file.readline().rstrip())
#
# for line in file:
# line = line.rstrip()
# if line != "":
# print(file.lineno(),":",line)
#-----------------------********数据&存储********-----------------
#处理json格式数据
# loads json -> 字典
# dumps 字典->json
# import json
# peo = {
# 'name':"强月城",
# 'age':22,
# 'aihao':"huangyan"
# }
# js = json.dumps(peo,ensure_ascii=False) #正常显示中文
# print(js) #{"name": "\u5f3a\u6708\u57ce", "age": 22, "aihao": "huangyan"}
# print(type(js)) #<class 'str'>
#
# data = json.loads(js)
# print(data) #{'name': '强月城', 'age': 22, 'aihao': 'huangyan'}
# 转换为类实例
# class People:
# def __init__(self,d):
# self.__dict__ = d
#
# js = {"name": "\u5f3a\u6708\u57ce", "age": 22, "aihao": "huangyan"};
# peo = People(js)
# print(peo.name)
# print(peo.age)
# print(peo.aihao)
# 强月城
# 22
# huangyan
# SQLite
#MySQL
# pip install pymysql
# connect 连接数据库 cursor 操作数据库的cursor对象 execute执行sql语句 commit 提交对数据库的修改 rollback回滚
# import pymysql
# import json
# def connectDB():
# db = pymysql.connect("127.0.0.1",user="root",passwd="123456",db="db_lcbk",charset='utf8')
# return db
# 创建persons表
# def createTable(db):
# cursor=db.cursor()
# sql='''CREATE TABLE persons
# (id INT PRIMARY KEY NOT NULL,name TEXT NOT NULL,age INT NOT NULL,address CHAR(50),salary REAL);
# '''
# try:
# cursor.execute(sql)
# db.commit()
# return True
# except:
# return False
# db = connectDB()
# createTable(db)
#插入四条数据
# def insertRecords(db):
# cursor=db.cursor()
# try:
# cursor.execute("INSERT INTO persons (id,name,age,address,salary) VALUES (2,'强月城',22,'zhongguo',2000.000)")
# db.commit()
# return True
# except:
# return False
# print(insertRecords(db))
#删除
# def delete(db):
# cursor = db.cursor()
# try:
# cursor.execute("delete from persons")
# db.commit()
# return True
# except:
# return False
# delete(db)
# 查询
# def select(db):
# cursor = db.cursor()
# try:
# cursor.execute("select * from persons where age>=21 order by age desc")
# result = cursor.fetchall()
# print(result)
# fields = ["id","name","age","address","salary"]
# records = []
# for row in result:
# records.append(dict(zip(fields,row)))
# return json.dumps(records,ensure_ascii=False)
# except:
# print("出错了")
# return None
# print(select(db))
# ((3, '强月城', 22, 'zhongguo', 2000.0), (2, '强', 21, 'zhongguo', 2000.0))
# [{"id": 3, "name": "强月城", "age": 22, "address": "zhongguo", "salary": 2000.0},
# {"id": 2, "name": "强", "age": 21, "address": "zhongguo", "salary": 2000.0}]
# --------------******************mongodb*****************---------------
# import pymongo
# client = pymongo.MongoClient(host='101.200.201.196', port=27017)
# mydb = client["mydatabase"] #创建数据库 指定数据库
# mycol = mydb["myList"] #创建集合 指定集合
#--------***********增****--------
# mydict = {"name":"hy","age":20,"sex":"女"}
# x = mycol.insert(mydict) #插入
# y = mycol.insert_one(mydict) #返回ID插入 5f904fc225a54e6c9f90263e
# print(y.inserted_id)
# z = mycol.insert_many([{"name": "q1", "age": 1, "sex": "男"}, #插入多个
# {"name": "q2", "age": 2, "sex": "男"},
# {"name": "q3", "age": 3, "sex": "男"},
# {"name": "q4", "age": 4, "sex": "男"}])
# z = mycol.insert_many([{"_id":1,"name": "q1", "age": 1, "sex": "男"}, #插入多个指定ID
# {"_id":2,"name": "q2", "age": 2, "sex": "男"},
# {"_id":3,"name": "q3", "age": 3, "sex": "男"},
# {"_id":4,"name": "q4", "age": 4, "sex": "男"}])
# print(z.inserted_ids)
#[ObjectId('5f9050d72896de5fe77ea66d'), ObjectId('5f9050d72896de5fe77ea66e'),
# ObjectId('5f9050d72896de5fe77ea66f'), ObjectId('5f9050d72896de5fe77ea670')]
# print(client.list_database_names()) #查看所有数据库
# print(mydb.list_collection_names()) #查看数据库集合
#---------*******删******---------
# myque = {"name":"q1"}
# mycol.delete_one(myque)
# list = {"name":"q2"}
# mycol.delete_many(list)
# 删除所有
# my = {}
# mycol.delete_many(my)
#删集合
# mycol.drop()
#---------******改******---------
# old = {"name":"qyc"}
# new = {"$set":{"name":"love"}}
# x = mycol.update_one(old,new) #修改一条 many修改多条
#
# a = mycol.find()
# for i in a:
# print(i)
#--------*******查*****----------
# x = mycol.find_one() #查一条
# print(x)
# y = mycol.find() #查全部
# for i in y:
# print(i)
# z = mycol.find({},{"name":1}) #不能同时设置1和0 1代表查询 0代表去掉字段
# for i in z:
# print(i)
# {'_id': 1, 'name': 'q1'}
# {'_id': 2, 'name': 'q2'}
# {'_id': 3, 'name': 'q3'}
# {'_id': 4, 'name': 'q4'}
# 指定条件
# a = mycol.find({"name":"qyc"})
# for i in a:
# print(i)
# {'_id': ObjectId('5f904dfe47ed0fafe0de9ef4'), 'name': 'qyc', 'age': 22, 'sex': '男'}
# b = mycol.find({"age":{"$gt":3}}) #大于3
# for i in b:
# print(i)
# 指定条数
# a = mycol.find().limit(4)
# for i in a:
# print(i)
#-------------*********TCP于UDP编程**********----------
#客户端套接字 socket
# 服务端套接字 socketserver
#TCP 面向连接的 SOCK_STREAM
#UPD 无连接 SOCK_DGRAM
# 步骤
from socket import *
# 1.创建socket对象
host = ""
bufferSize = 102400
prot = 1314
addr = (host,prot)
tcpServerSoctet = socket(AF_INET,SOCK_STREAM) #AF_INET IPv4 AF_INET6 IPv6 STREAM TCP
# 2.绑定端口号
tcpServerSoctet.bind(addr)
# 3.监听端口号
tcpServerSoctet.listen()
print("server port: 1314 正在等待客户端连接")
# 4.等待客户端socket的连接
tcpClientSocket,addr = tcpServerSoctet.accept()
print("客户端已经连接","addr:",addr)
# 5.读取客户端发送过来的数据
data = tcpClientSocket.recv(bufferSize)
print(data.decode('utf8'))
# 6.向客户端发送数据
tcpClientSocket.send('你好socket.\n'.encode(encoding='utf-8'))
# 7.关闭客户端socket连接
tcpClientSocket.close()
# 8.关闭服务端socket连接
tcpServerSoctet.close()