随笔分类 - python
摘要:# numpy的数据类型,类型转换,占用内存字节数f64 = numpy.float64(42)print(f64, type(f64), f64.dtype, f64.dtype.itemsize)i8 = numpy.int8(42.0)print(i8, type(i8), i8.dtype,
阅读全文
摘要:import subprocesscalcProc = subprocess.Popen('c:\\Windows\\System32\\calc.exe')subprocess.Popen(['C:\\Windows\\notepad.exe', 'C:\\hello.txt']) 带有参数sub
阅读全文
摘要:import threading, timeprint('Start of program.')def takeANap(): time.sleep(5) print('Wake up!')threadObj = threading.Thread(target=takeANap)threadObj.
阅读全文
摘要:在Python 中,日期和时间可能涉及好几种不同的数据类型和函数。下面回顾了表示时间的3 种不同类型的值: Unix纪元时间戳(time模块中使用)是一个浮点值或整型值,表示自 1970年1月 1日午夜 0点(UTC)以来的秒数。 datetime 对象(属于datetime 模块)包含一些整型值,
阅读全文
摘要:import json, pprint# 支持的类型:字典、列表、整型、浮点型、字符串、布尔型或Nonedef LoadsAndDumps(): stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felin
阅读全文
摘要:import csv, pprintdef ReadFile2List(): file = open('example.csv') reader = csv.reader(file) data = list(reader) pprint.pprint(data) print(data[0][2])
阅读全文
摘要:import shutil, os, zipfile # 复制、移动、改名、删除文件与文件夹 #shutil.copy('e:\\111\\key.txt', 'd:\\111\\') # copy文件 #shutil.copytree('e:\\111', 'd:\\111\\aaa') # co
阅读全文
摘要:import pprint#格式化打印message = 'It was a bright cold day in April, and the clocks were striking thirteen.'count = {}for character in message: count.setd
阅读全文
摘要:import re # 正则模块def regular(): data = "She is more than pretty. 520" # 正则 reg = r"mo" # 指定字符 => span=(7, 9), match='mo' reg = r"." # (.)单个字符 => span=(
阅读全文
摘要:import shelve# shelve_demo.py 持久性字典:Python对象的持久化# 键值对形式, 将内存数据通过文件持久化, 值支持任何pickle支持的Python数据格式# 与pickle的主要区别是键值对方式, 并且在目录下生成三个文件class Person(object):
阅读全文
摘要:import pickle# 序列化 用于对Python对象进行序列化和反序列化的二进制协议f = open("pickle.txt", "wb+")lists = [123, "中文", [456]]strs = "字符串"num = 123# 写入pickle.dump(lists, f) #
阅读全文
摘要:import os path = os.getcwd() # 获取当前目录print("路径: {}".format(path)) # 路径: E:\python\练习\笔记dirname = os.path.dirname(path) # 获取文件夹名print("文件夹名为: {}".forma
阅读全文
摘要:import osimport signal # 执行命令dirs = os.popen("dir").read()print(dirs)# 打印目录树dirs_info = os.scandir()for info in dirs_info: print("文件名: {}, 路径: {}, ino
阅读全文
摘要:import calendarimport timecalen_text = calendar.TextCalendar()# 打印月历calen_text.prmonth(2017, 5, w=0, l=0)# 打印年历calen_text.pryear(2017, w=2, l=1, c=6,
阅读全文
摘要:import datetime import time datetime_dt = datetime.datetime.today() # 获取当前日期和时间, 2017-10-26 10:03:28.693198datetime_str = datetime_dt.strftime("%Y/%m/
阅读全文
摘要:import timedef time_demo(): curtime = time.time() # 获取当前时间戳, 1508982743.220433 time_str = time.ctime(curtime) # 转为string格式, 'Thu Oct 26 09:52:23 2017'
阅读全文
摘要:import random # 随机数模块 lists = [1, 2, 3, 4, 5] def demo(): # 产生[0, 100]随机整数 num = random.randint(0, 100) print(num) # 产生[0, 100)随机浮点数 fnum = random.uni
阅读全文
摘要:import sys# 捕获异常 (可灵活组合) def excep(): # - try except - try: print(ex) except: # 捕获所有异常 print("捕获异常!") try: print(ex) except: # 通过函数获取异常信息 types, value
阅读全文
摘要:# 调用自定义模块 #coding=utf-8# mymodule.py 自定义模块def myfunction(): return "myFunction"# 避免外界调用函数时运行了测试代码if __name__ == "__main__": print(myfunction()) # 导入模块
阅读全文
摘要:# sorted(iterable,key=None,reverse=False)# key接受一个函数,这个函数只接受一个元素,默认为None# reverse是一个布尔值。默认为False排在前,True排在后,升序,即符合条件的往后排# 按照年龄来排序students = [('john',
阅读全文

浙公网安备 33010602011771号