一、os模块

import os
# os.rename(old,new)#重命名
# os.remove(f)#删除文件 # os.mkdir('china/beijing') #创建文件夹
# os.makedirs('china/beijing') #父目录不存在的时候会帮你创建
# os.removedirs('china')#只能删除空文件夹 # print(os.listdir())#显示该目录下面的所有文件和文件夹
# print(os.path.isdir('china1'))#判断是否是文件夹
# print(os.path.isfile('china'))#判断是否是文件
# print(os.path.exists('china'))#判断文件或者文件夹是否存在
# res = os.system('ipconfig')#执行操作系统命令 ls]
# print('res...',res)
# res = os.popen('ipconfig').read()#用来执行操作系统命令
# print(res)
# print(os.path.join('china','beijing','haidian','changping','a.py'))#拼路径
res = os.path.split(r'china\beijing\haidian\changping\a.py')#用来分割文件名和路径
# print(res)
res = os.path.dirname(r'china\beijing\haidian\changping\a.py')#取父目录
print(res)
print(os.path.getsize('笔记.txt'))#单位是字节
print(os.getcwd())#取当前的目录
print(os.chdir(r'C:\Users\nhy\PycharmProjects\jnz\day5'))#进入到哪个目录下
print(os.getcwd())#取当前的目录
#分别用他们2个创建一个2层的牡蛎 # china/beijing # 统计e盘下面有多个python文件 # res = os.walk(r'china')
# count = 0
# for cur_path,dirs,files in res:
# print(cur_path)
# #china/a.py
# for f in files:
# if f.endswith('.py'):
# # count+=1
# os.remove(os.path.join(cur_path,f))
# print(count)
#
#
# def find_file(path,keyword):
# #查找文件的
# res = os.walk(path)
# for cur_path, dirs, files in res:
# for file_name in files:
# if keyword in file_name:
# print('该文件在 %s下面' %cur_path)
#
#e:\\xxx"\xxx
#/user/loal/xxx 二、time模块
#和时间相关的操作

import time

# time.sleep(30)

# 格式化好的时间 20180915 14:08:53

# 时间戳  从计算机诞生那天到现在过了多少秒

# res = time.strftime('%Y-%m-%d %H:%M:%S') #取当前的格式化日期
res = time.time()#获取当前的时间戳 #时间元组 #先把格式化好的时间转成时间元组 #1992-01-02 # time_tuple = time.strptime('2038-08-29 19:23:59','%Y-%m-%d %H:%M:%S')#把格式化好的时间转成时间元组
# print(time.mktime(time_tuple)) def str_to_timestamp(time_str=None,format='%Y%m%d%H%M%S'):
#格式化好的时间转时间戳的
#不传参数的话返回当前的时间戳
if time_str:
time_tuple = time.strptime(time_str, format) # 把格式化好的时间转成时间元组
timestamp = time.mktime(time_tuple)
else:
timestamp = time.time()
return int(timestamp) # print(str_to_timestamp())
# print(str_to_timestamp('20391123175123'))
# print(str_to_timestamp('2013-08-09','%Y-%m-%d')) #时间戳转格式化好的时间 #先把时间戳转成时间元组 # res = time.gmtime(time.time())#是把时间戳转时间元组的,标准时区
res = time.localtime(time.time())#是把时间戳转时间元组的,当前时区
res2 = time.strftime('%Y-%m-%d %H:%M:%S',res)
print(res2)
def timestamp_to_strtime(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
#这个函数是用来把时间戳转成格式化好的时间
#如果不传时间戳的话,那么就返回当前的时间
if timestamp:
time_tuple = time.localtime(timestamp)
str_time = time.strftime(format,time_tuple)
else:
str_time = time.strftime(format)
return str_time #用当前的时间戳+50年的秒数,时间戳转成格式化好的时间
five = str_to_timestamp() - (50*365*24*60*60)
res = timestamp_to_strtime(five)
print('50年后的时间是',res) 三、加密的模块
# 8a10818ac51be3681d5f915041c90092

import hashlib

# import md5 #这个是python2里面的
password='123123'
# print(password.encode())#转成二进制类型的才可以加密
m = hashlib.md5(password.encode())
# m = hashlib.sha1(password.encode())
# m = hashlib.sha224(password.encode())
# m = hashlib.sha256(password.encode())
# print(dir(m))
print(m.hexdigest())
#md5加密之后是不可逆 #撞库
# 123456 4297f44b13955235245b2497399d7a93
# 345678 4297f44b13955235245b2497399d7a93
# 123123 4297f44b13955235245b2497399d7a93 # 123123 #123456+34few45834@$ def my_md5(s:str,salt=None):
#salt是盐值
s = str(s)
if salt:
s = s+salt
m = hashlib.md5(s.encode())
return m.hexdigest() #加盐 #34few45834@$ 四、第三方模块
# pip install xpinyin
# import xpinyin
# s = xpinyin.Pinyin()
# pinyin = s.get_pinyin('倪菊芳','')
# print(pinyin) #1、ip host='118.24.3.40'
user='jxz'
password='123456' #密码只能是字符串
db='jxz'
port=3306#端口号只能写int类型
charset='utf8'#只能写utf8,不能写utf-8
import pymysql
conn = pymysql.connect(host=host,password=password,
user=user,db=db,port=port,
charset=charset,autocommit=True
)#建立连接 cur= conn.cursor() #建立游标
# cur.execute()#只是帮你执行sql语句
# print(cur.fetchall())#获取数据库里面的所有的结果
# print('fetchone',cur.fetchone())
# sql='insert into app_myuser (username,passwd,is_admin) VALUE ("python123456","123456",1);'
sql='select * from app_myuser limit 5;'
cur.execute(sql)
print(cur.description)#获取这个表里面的所有字段信息
# conn.commit()#提交
cur.close()
conn.close() def my_db(ip,user,password,db,sql,port=3306,charset='utf8'):
conn = pymysql.connect(
host=ip,user=user,password=password,
db=db,
port=port,charset=charset,autocommit=True
)
cur = conn.cursor()
cur.execute(sql)
res = cur.fetchall()
cur.close()
conn.close()
return res def my_db2(sql):
conn = pymysql.connect(
host='118.24.3.40',user='jxz',password='123456',
db='jxz',
port=3306,charset='utf8',autocommit=True
)
pass
五、excel操作
1、写excel
import xlwt
import xlrd
import xlutils #写Excel book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')
# sheet.write(0,0,'id')#指定行和lie写内容
# sheet.write(0,1,'username')
# sheet.write(0,2,'password')
#
# sheet.write(1,0,'1')
# sheet.write(1,1,'niuhanyang')
# sheet.write(1,2,'123456') #
stus = [
[1,'njf','1234'],
[2,'xiaojun','1234'],
[3,'hailong','1234'],
[4,'xiaohei','1234'],
[4,'xiaohei','1234'],
[4,'xiaohei','1234'],
[4,'xiaohei','1234'],
[4,'xiaohei','1234'],
[4,'xiaohei','1234'],
]
line = 0 #控制的是行
for stu in stus:#行
#stu [1,'njf','1234']
col = 0
for s in stu:
#0 0 1
#0 1 njf
#0 2 1234 # 1 0 2
# 1 1 xiaojun
# 1 2 1234 sheet.write(line,col,s)
col+=1
line+=1 book.save('stu.xls')# .xlsx 2、读excel
import xlrd

book = xlrd.open_workbook('stu.xls')
sheet = book.sheet_by_index(0)
# sheet = book.sheet_by_name('sheet1')
print(sheet.nrows) #excel里面有多少行
print(sheet.ncols) #excel里面有多少列 print(sheet.cell(0,0).value) #获取到指定单元格的内容
print(sheet.cell(0,1).value) #获取到指定单元格的内容 print(sheet.row_values(0))#获取到整行的内容
# print(sheet.col_values(0))#获取到整列的内容 for i in range(sheet.nrows):#循环获取每行的内容
print(sheet.row_values(i)) 3、修改excel
import xlrd

# from xlutils import copy
# book = xlrd.open_workbook('stu.xls')
# #先用xlrd打开一个Excel
# new_book = copy.copy(book)
# #然后用xlutils里面的copy功能,复制一个Excel
# sheet = new_book.get_sheet(0)#获取sheet页
# sheet.write(0,1,'倪菊芳')
# sheet.write(1,1,'白小军')
# new_book.save('stu.xls')
d = {'喝一瓶': 30, '喝3瓶': 50, '不喝': 1, '买单': 19} def choice(d):
print('喝一瓶...') choice(d) def export_excel(table_name):
pass
#user.xls


												

python基础(五)的更多相关文章

  1. Python基础(五) python装饰器使用

    这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 # -*- coding:gbk -*- '''示例1: 最简单的函数,表示调用了两次 ...

  2. 【笔记】Python基础五:装饰器

    一,什么是装饰器 本质就是函数,功能是为其他函数添加附加功能 原则: 1,不修改被修饰函数的源代码 2,不修改被修饰函数的调用方式 例子: import time def timmer(func): ...

  3. python基础五

    列表生成式 将列表data=[1,2,3]里的元素都乘2 方法一 data=[1,2,3] for index,i in enumerate(data): data[index] *=2 print( ...

  4. python基础(五)缩进和选择

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 缩进 Python最具特色的是用缩进来标明成块的代码.我下面以if选择结构来举例. ...

  5. python基础(五)列表,元组,集合

    列表 在python中是由数个有序的元素组成的数据结构,每一个元素对应一个index索引来隐式标注元素在列表中的位置.是python中最常用的一种数据类型.需要注意的是列表中可以有重复相同的数据. 列 ...

  6. python基础五--dict

    一.昨日内容回顾 1. 列表: 增 insert:按照指定下标插入 append:在列表最后增加 extend:迭代式的增加到列表的最后,相当于列表扩展 删 pop:根据指定下标删除,返回删除的元素, ...

  7. python基础五之字典

    python数据的可变性 通过数据的可变性,可将数据分为可变数据类型和不可变数据类型. 可变数据类型:list,dict (不可哈希) 不可变数据类型:元祖,bool,int,str (可哈希) py ...

  8. python基础五(函数、全局变量和局部变量)

    一.全局变量和局部变量 全局变量,即可以在当前文件所有地方都可使用的变量,一次修改,整个文件都影响.如果函数体内部使用全局变量,要先声明global 局部变量,只能在函数体内部使用,除了函数体就不可使 ...

  9. python 基础(五) 迭代器与生成器

    迭代器和生成器 迭代器 iterator (1) 迭代对象: 可以直接作用于for循环的 称为可迭代对象(iterable)可以通过 isinstance 判断是否属于可迭代对象 可以直接作用于for ...

  10. Pyhton开发【第五篇】:Python基础之杂货铺

    Python开发[第五篇]:Python基础之杂货铺   字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进 ...

随机推荐

  1. Spring AOP 的实现机制

    作者:大名Dean鼎 http://www.importnew.com/28342.html AOP(Aspect Orient Programming),一般称为面向切面编程,作为面向对象的一种补充 ...

  2. Excel关联xml文件

    1.新建没传值的xml文件,变量名称自己定义好 2.打开excel,如果之前没有设置过,点击选项 如果当前Excel菜单栏中没有开发工具项,在自定义功能区先勾选上开发选项 3.点右下角的xml映射 弹 ...

  3. Python2.x爬虫入门之URLError异常处理

    大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: (1)网络无连接,即本机无法上网 (2)连 ...

  4. 数据结构复习之Vector

    /** * The number of times this list has been <i>structurally modified</i>. * Structural ...

  5. 1、Python2.7编译安装

    下载Python安装包(下载地址:https://www.python.org/downloads/) 选择Python 2.7.3版本 防止交互式模式下出现方向键乱码问题,需安装相关包 yum in ...

  6. php留言板程序

    =================== 先创建note.php <html><head><title>PHP留言本</title></head&g ...

  7. 魅族pro 7详细打开Usb调试模式的方法

    经常我们使用安卓手机链上Pc的时候,或者使用的有些APP比如我们公司营销小组经常使用的APP引号精灵,之前老版本就需要开启usb开发者调试模式下使用,现经常新版本不需要了,如果手机没有开启usb开发者 ...

  8. 机器学习sklearn

    sklearn相关模块导入 from sklearn.feature_extraction import DictVectorizer from sklearn.feature_extraction. ...

  9. springboot快速开发(简单web)

    这是一个springboot基础配置文件介绍的demo.只涉及到 控制层与前端的简单交互,用于验证代码的畅通. spring-boot  pom.xml解释 <?xml version=&quo ...

  10. mongo aggregate 删除重复数据

    $group 按照什么排序, 关照 _id 这个是排序的依据 $match 这个从排序的结果内抽取 count 大于一的 allDiskUse  如果内存配置比较小, 设置这个才能运行, 否则会崩. ...