您的位置:首页 > 编程语言 > Python开发

python知识点8--目录操作及时间

2018-01-14 11:29 399 查看
知识点8---目录操作及时间

  1 目录的操作02

   import os

   获取绝对路径:

   absPath = os.path.abspath("情书")

   print(absPath)

   路径的拼接

      拼接方式1

             strPath = r"绝对路径"  I:sz1704\day09\code

             fileName = r"文件名"   情书.txt

             filePath = strPath + fileName

             print(filePath)

      拼接方式2,一般用join

            join方法用来拼接路径和文件名

            filePath = os.path.join(r"I:sz1704\day09\code","情书.txt")

             print(filePath)

   拆分

     1 将完整的路径拆分成文件名 和文件所在路径

       结果用元组包裹起来

           res = os.path.split("I:sz1704\day09\code\情书.txt")

          print(res)

     2 将完整的文件名拆分为扩展名 和文件名(不包括扩展名)

       结果用元组包裹起来

           res = os.pash.splitext("情书.txt")

           print(res)

    判断是否是目录

           strPath1 = r"I:sz1704\day09\code"

           strPath2 = r"I:sz1704\day09\code\情书.txt"

           res = os.path.isdir(strPath1)

           print(res)

   判断是否是文件

           res = os.path.isfile(strPath2)

           print(res)

   判断目录或文件是否存在

              res = os.path.exists(strPath1)

                                                  True

              res = os.path.exists(strPath2)

              print(res)

2 数据的持久性模块 PICKLE

            把(变量)列表,元组,字典  对象保存在文件中叫可序列化

            从文件中的创建上一次程序保存的对象,叫反序列化

import pickle

   1 序列化---将对象保存到文件中去             存

           list1 = ["奥巴马","本拉登","达赖","金三胖胖","安倍狗"]

           tuple1 = ("奥巴马","本拉登","达赖","金三胖胖","安倍狗")

           wf=open("date.txt","wb",encoding="utf-8")

           参数1 obj:需要保存的对象名

           参数2 file:要保存的文件(已经打开的)

           pick.dump(list1,wf)

           pick.dump(tuple1,wf)

           wf.close()

    2 反序列化 ---将文件中的数据读取出来,转换成对象  取

          参数1 file:要读取的文件(已经打开的)

           rf = open("data.txt","rb")

           res = pickle.load(rf)

           print(res)

           print(type(res))

           rf.close()

     3 时间  

        简写UTC:格林威治时间,世界标准时间,世界协调时间 

         ---UTC+8(北京时间,东八区,比世界标准时间快8小时)

        DST 夏令时:是一种为了节约资源,而人为规定的时间,一般夏天调早一小时

        例子

           时间格式:2018/1/5

                     2018年1月5日  09:45:25

                     2018-01-05    08:48:00

         客户看到的会有多种格式---字符串

     计算机里  

      时间的表现形式

              时间戳:以秒为单位  计算机表示时间的形式

                   是一个以整数或者浮点数表示的一个时间差,

                   相对于    1970年1月1日0点0分0秒

              时间元组:负责时间戳及字符串的转换    (python菜鸟教程)

       

                        年  tm_year

                        月  tm_month    1到12

                        日  tm_mday      1到31

                        时  time_hour   0到23

                        分  time_min    0到59

                        秒  time_sec    0到59

                        一周中的第几天  0到6   0表示周一

                        一年中的第几天  1到366

                        是否是夏令时    

              字符串:2018/1/5  ,

                      2018-01-05  08:48:00 ,

                      2018年1月5日  09:45:25

时间的格式化:

                  

import time

    获取当前的时间戳:是一个浮点数,单位是 秒

          time1 = time.time()

          print(time1)

将时间戳转换为时间元组

    gmtime    -----  世界标准时间

    localtime -----  北京时间

    gmtime 空参时,默认是将当前时间转换为时间元组

 

    gmtime 是将时间戳转换为世界标准时间(UTC)

    #tupleTime = time.gmtime()

     tupleTime = time.gmtime(time1)

     print(tupleTime)

     localtime是将时间戳转换为本地时间(UTC+8)

     空参时,默认是将当前本地时间转换为时间元组

     tupleTime = time.localtime()

     print(tupletime)

      表示昨天的时间

      yesDayTime = time.time()-60*60*24

      tupleTime = time.localtime(yesDayTime) 

       print(tupleTime)

时间元组转换为时间戳   mktime

      tupleTime = time.localtime()

       不能是空参

       mktime需要传递进一个时间元组参数,但会自动舍弃掉小数位

       time2 = time.mktime(tupleTime)

时间元组转换为字符串

        time.strftime

    参数1 : 是时间需要展示的格式

    参数2:   要转换的时间元组  tupleTime = time.localtime()

 strTime = time.strftime("%Y-%m-%d %H:%M:%S",tupleTime)

 strTime = time.strftime("%Y/%m/%d",tupleTime)

 strTime = time.strftime("%x %X",tupleTime)

   中文的年月日

    strTime = time.strftime("%Y{y}%m{m}%d{d}",tupleTime.formate(y="年",m="月",d="日"))

    print(strtime)

 

字符串转换为时间元组

     time.strptime

参数1:字符串形式的 时间   2018-01-05 11:59:17

参数2:时间字符串的格式,该格式需要与字符串形式的时间对应

 strTime = time.strptime("2018-01-05 11:59:17",%Y-%m-%d  %H:%M:%
4000
S)

   中文时间

   2018年1月5日

   tupleTime = time.strptime("2018年01月05","%Y{y}%m{m}%d{d}".formate(y="年",m="月",d="日"))

   print(tupleTime)

import time

   需求:打印1-100,隔一秒打印

         for i in range(1,101):

         #让程序睡一会,单位: 秒

         #会阻塞程序的运行

         #此处让程序睡一会,老板加钱再优化
time.sleep(1)
print(i)

    统计程序运行的时间

       startTime = time.time()

       count = 0

       for i in range(1,100001):

             count += i

       endTime = time.time()

       runTime = endTime - startTime

       print(runTime)

     统计运行时间,第一次调用clock的时间接近0,后面再调用clock时

     是表示的与第一次的时间差

        startTime = time.clock()

        print(startTime)

        time.sleep(2)

        endTime = time.clock()

        print(endtime)

        time.sleep(5)

        endTime = time.clock()   相当于第一次的时间差

        print(endTime)

05 字符串的使用

 1字符串的大小写判断

  

   strDate = "wen"

# 把字符串全部转换成大写

   print(strDate.upper())

把字符串转换为小写

   strDate1 = "WEN IS VERY GOOD"

   strDate1 = "WEN IS very GOOD"

   strDate1 = "WEN IS VERY GOOD 888 温"

把字符串中的字母全部转换为小写

   print(strDatelower())

首字母大小写

每个英文单词的首字母大写

   strDate2 = "wen is 88a good man"

   print(strDate2.title())

 

将一个字符串的首字母大写

   print(strDate2.capitalize())

大小写切换

   strDate3 = "Wen is VERY GOOD"

   print(strDate3.swapcase())

判断字符串中的字母是否全部是大写

   strDate4 = "wen Is GOod man!88,温"

   strDate4 = "WEN IS VERY GOOD"

   strDate4 = "WEN IS VERY GOOD!88,温"

   print(strDate4.isupper())

判断字符串中的字母是否全部是小写

    strDate5 = "wen Is GOod man!88,温"

    strDate5 = "wen is good man"

    strDate5 = "wen is good man!88,温"

    print(strDate5.islower())

判断是否是title

    strDate6 = "wen Is GOod man!88,温"

    strDate6 = "Wen Is Good Man "

    strDate6 = "Wen Is GOOD Man "

    strDate6 = "Wen Is 888Good Man "

    print(strDate6.istitle())

6 字符串的位置

  "左边缘对齐              "

  "              右边缘对齐"

  strDate1 = "标题"

  让字符串内容居中

   一个参数表示设置字符串的长度,并让原字符串的内容居中

   空出的用空格填充

       print(strDate1.center(60))

第二个参数指定空白的位置用什么字符填充

      print(strDate1.center(60,"*"))

      strDate1 = "字符串左边缘对齐"

      print(strDate2.ljust(60,"*"))   填充的依然是空白位置

      strDate3 = "字符串右边缘对齐"

      print(strDate3.rjust(60,"*"))

7 字符串的提取

      strDate = "      wenXxX"

 

去除字符串边上的

      strDate = "********wenXXX******"

      print(strDate)

默认是去除字符串两边的空格

      print(strDate.strip())

参数用来指定去除字符串两边的字符

      strDate = "********wenXXX******"

      strDate = "********wen*XXX******"

      print(strDate.strip("*"))

去除字符串左边的字符,默认是去除空格

   print(strDate.lstrip("*"))

去除字符串右边的字符,默认是去除空格

   print(strDate.rtrip("*"))

8 字符串的查找

   字符串当中特殊字符的查找

    strDate = "wen is a very very very good man"

    pathDate = "I:\sz1704\day09\code"

    从原字符串中找到指定的字符串,

    找到后会返回第一个匹配项首字母的索引

     如果没有找到返回-1

           print(strDate.find("g"))

           print(strDate.find("goo"))  

           print(strDate.find("god"))  得-1

           print(strDate.find("very")) 得9

     参数2表示查找范围的起始位置,

     参数3表示查找范围的结束位置

        print(strDate.find("very",16,23 ))  还是19

     从字符串的右边开始找,结果不变

          print(strDate.rfind("very"))        还是19

   查找 index 与find类似,从原字符串中找到指定的字符串,

                   找到后会返回第一个匹配项首字母的索引

 index如果没有找到,会报错

 find如果没有找到,会返回-1

   strDate = "wen is a very very very good man"

   print(strDate.index("vey")

   print(strDate.rindex("very")

   print(strDate.index("very",16,27 ))

9 字符串的拆分,拼接

strData = "wen is a good man wen is nice man"

获取所有的单词:

   以空格为拆分的字符,

   split表示以指定的字符将原字符拆成多个字符串

   多个字符串会放在一个列表中

   空参,即默认情况是以空格拆分

   listStr = strData.split()

   print(listStr)

   strData = "wen is a good man, wen is nice man"

   listStr = strData.split(",")

   print(listStr)

如果指定的切割字符不存在,

则会把原字符串作为列表的一个元素

   listStr = strData.split("!")

   print(listStr)

strdata = "wen in a good Man

           wen in a nice Man 

           wen in a handsome Man "

把字符串切分成一行一行的数据

默认是不包含换行符

listStr = strData.splitlines()

如果指定参数,表示是否保留换行符,True是保留

listStr = strData.splitlines(True)

print(listStr)

拼接

listStr = ["wen in a good Man","wen in a good Man","wen in a good Man"]

表示以指定的字符,如,来拼接列表中的所有元素

组成一个新的字符串

strData = ",".join(listStr)

print(strData)

strData = "   ".join(listStr)

print(strData)

strData = "".join(listStr)

print(strData)

10 字符串的替换

  strData = "wen is a good ,wen is very nice man,wen is a handsome man,wen is a XXX"

   old需要被替换掉的原字符

   new 用来替换的新字符串

   count 默认是全部替换,默认值是-1,数值表示替换的次数

   newData = strData.replace("wen","小灰灰")

   newData = strData.replace("wen","小灰灰",3)

   print(newData)

11字符串的杂

   判断字符串是否以某个字符串开始,

  (还可以有start参数跟end参数来确定需要判断的范围)

例子 判断网址是否以什么开始   

   strData = "www.baidu.com"

   strData = "ww.baidu.com"

   strData = "wwww.baidu.com"

   res = staData.startwith("www.")

   print(res)

   判断字符串是否以某个字符串结尾

 (还可以有start参数跟end参数来确定需要判断的范围)

   strData = "wwww.baidu.c"

   strData = "wwww.baidu.ccom"

   res = staData.endwith(".com")

   print(res)

统计字符出现的次数

  strData = "wen is a good ,wen is very nice man,wen is a handsome man,wen is a XXX"

  print(strData.count("wen"))

   print(strData.count("man"))

字符的操作   字母,数字,其他特殊字符---ascii码

strD = "y"    中间的十进制

把这个字母转换为ascii码

num = ord("strD")

print(num)

if "A" < = strD and strD <= "Z":

if 65 <= num and num <=90:

将ascii转换为字符

print(chr("65"))----------A

eval可以将字符当作表达式执行

eval("12=12")

print(list("wen is a good man"))

转换为  int,  str, list, tuple,.......
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: