zoukankan      html  css  js  c++  java
  • python基础(五)

    一、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'chinaeijinghaidianchangpinga.py')#用来分割文件名和路径
    # print(res)
    res = os.path.dirname(r'chinaeijinghaidianchangpinga.py')#取父目录
    print(res)
    print(os.path.getsize('笔记.txt'))#单位是字节
    print(os.getcwd())#取当前的目录
    print(os.chdir(r'C:Users hyPycharmProjectsjnzday5'))#进入到哪个目录下
    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








  • 相关阅读:
    《SQL 基础教程》第五章:复杂查询
    Ruby on Rails Tutorial 第一章笔记
    《Practical Vim》第十章:复制和粘贴
    《Practical Vim》第五章:命令行模式
    《SQL 基础教程》第四章:数据更新
    用户的三次登录验证及进度条
    socket模块开启一个永久性服务
    TCP协议实现切换目录
    爬取好友微信信息
    TCP协议中传输系统命令及上传下载文件
  • 原文地址:https://www.cnblogs.com/mengmeng1011/p/9745951.html
Copyright © 2011-2022 走看看