随笔分类 -  Python基础

摘要:Java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public static void dayByDay() throws Exception { String str1 阅读全文
posted @ 2020-05-15 11:44 CherryYang
摘要:import os os.makedirs('glance/api') os.makedirs('glance/cmd') os.makedirs('glance/db') ls = [] ls.append(open('glance/__init__.py','w')) ls.append(ope 阅读全文
posted @ 2020-05-09 19:33 CherryYang
摘要:import shelve set1 = {'a', 'b', 'c', 'd'} f1 = shelve.open("sh_file.txt") f1['key'] = set1f1.close() f2 = shelve.open("sh_file.txt") print(f2['key'])f 阅读全文
posted @ 2020-05-08 14:53 CherryYang
摘要:import pickle # 还记得bytes类型吗? str1 = "世界" bytes2 = str1.encode('utf-8') print(bytes2) set1 = {'a', 'b', 'c'} print(pickle.dumps(set1)) # 序列化为bytes类型的 i 阅读全文
posted @ 2020-05-08 14:13 CherryYang
摘要:import json #在内存中 数字、字符串、列表、字典、元组 类型数据 序列化后的字符串 dic = dict([('A', '65'), ('B', '66'), ('C', '67')]) dic_str = json.dumps(dic) # dumps() 序列化为字符串 print( 阅读全文
posted @ 2020-05-08 13:45 CherryYang
摘要:import sys print(sys.path) # 以列表按先后顺序列出各python模块的所在路径 import模块时依次查询这些路径 print(sys.platform) # 返回操作系统名称 print(sys.version) # 获取Python解释程序的版本信息 import s 阅读全文
posted @ 2020-05-08 12:33 CherryYang
摘要:import os print(os.getcwd()) # 获取当前工作目录 print(os.listdir()) # 列表列出当前目录下的目录名和文件名 os.mkdir("tempdir") os.chdir("./tempdir") new_dir = os.getcwd() print( 阅读全文
posted @ 2020-05-08 10:52 CherryYang
摘要:import random # 随机整数 r = random.randint(1, 3) # 大于等于1 且小于等于3之间的整数 print(r) r = random.randrange(1, 10, 2) # 大于等于1 且小于10之间的奇数 print(r) # 随机小数 r = rando 阅读全文
posted @ 2020-05-08 09:39 CherryYang
摘要:from datetime import datetime # 创建datetime类型的数据 a = datetime(2019, 5, 1, 12, 12) print(a) b = datetime.now() # 获取当前本地时间 print(b) c = datetime.utcnow() 阅读全文
posted @ 2020-05-07 23:59 CherryYang
摘要:# 时间戳(timestamp 1970年1月1日0:0:0-当前时间 浮点数) 结构化时间->时间戳 mktime()方法 时间戳->结构化时间 location()方法 (格林尼治时间用 gmtime()方法)# 时间字符串(格式化的时间字符串 StringFormatTime) 【 常见需记忆 阅读全文
posted @ 2020-05-07 22:44 CherryYang
摘要:# 创建默认值字典 defaultdict()中传入的参数 必须是可被调用的 callable from collections import defaultdict dd = defaultdict(lambda: 'V3') # lambada表达式 【lambada 参数: 返回结果】 无参数 阅读全文
posted @ 2020-05-07 19:44 CherryYang
摘要:key键有序的字典 from collections import OrderedDict # 先回忆下字典的创建 dic1 = dict({}) # 空字典 {} dic2 = {'0': 48, '1': 50, '2': 51} # 键值对方式 {'0': 48, '1': 50, '2': 阅读全文
posted @ 2020-05-07 18:55 CherryYang
摘要:双端队列 deque 与 队列queue 双端队列-高效实现插入删除操作的双向列表(list列表的插入、删除效率较低) import queue q = queue.Queue() # 创建一个队列 (一端放,另一端取) FIFO (First In First Out) 先进先出 不能插队哦 没有 阅读全文
posted @ 2020-05-07 18:48 CherryYang
摘要:from collections import namedtuple circle_tuple = namedtuple('circle', ['x', 'y', 'r']) p = circle_tuple(1, 2, 5) print(p) # circle(x=1, y=1, r=5) # 元 阅读全文
posted @ 2020-05-07 18:02 CherryYang
摘要:import re s = "1 -2*(60+(-40.35/5)-(-4*3))" # 获取所有数字 ret = re.findall(r"\d+", s) print(ret) # ['1', '2', '60', '40', '35', '5', '4', '3'] # 小数不被拆分 针对小 阅读全文
posted @ 2020-05-06 22:17 CherryYang
摘要:import re ret = re.search(r"<(?P<body_info>\w+)>.*?</(?P=body_info)>", "<html><body><a href=#>跳转到头部</a><p>你点击试试</p></body></html>") # ()组的顺序号 获取想得到的内容 阅读全文
posted @ 2020-05-06 22:01 CherryYang
摘要:s = '''<head> <meta charset="UTF-8"> <title>学习HTML标签</title> <style></style> <link rel="stylesheet" href=""> <script></script> </head> <body> <h1>正文</ 阅读全文
posted @ 2020-05-06 18:38 CherryYang
摘要:id_str = input("输入一个身份证号:") import re obj = re.compile(r"^([1-9]\d{16}[0-9x]|[1-9]\d{14})$") # 将正则表达式编译成为一个 正则表达式对象 ret = obj.match(id_str) if ret: pr 阅读全文
posted @ 2020-05-06 16:50 CherryYang
摘要:import re s1 = '绿茶白茶黄茶青茶红茶黑茶' s2 = '中国绿茶白茶黄茶青茶红茶黑茶' ret = re.findall(".茶", s1) print(ret) r1 = re.search(".茶", s1) print("search方法直接返回", r1) if r1: pr 阅读全文
posted @ 2020-05-06 15:04 CherryYang
摘要:from urllib.request import urlopen import os.path def cache(func): def inner(*args, **kwargs): if os.path.getsize("web_cache"): with open("web_cache", 阅读全文
posted @ 2020-05-05 16:04 CherryYang