zoukankan      html  css  js  c++  java
  • python 学习

    • 字符处理
        • 字符截取
      #通过索引访问字符
      mystr="my string"
      mystr[0]     # 'm'
      mystr[-2]     #'n'
      
      #通过切片方式访问字符串的一部分
      mystr[1:4]     #'y s'
      mystr[3:]     #'string'
      mystr[-3:]     #'ing'
      
      #通过步长的方法获取
      mystr[:3:-1] #'gnirt'
      mystr[1::2]#'ysrn'
      
      #循环遍历
      for c in mystr:
      
      #乘法对字符串多次重复
      'xo'*3 #xoxoxo
      
      #使用splitlines 分隔多个单行字符串
      list=one_large_string.splitlines()
      
      #获取集合交集
      import sets
      magic_chars=sets.Set('abcabcasds')
      po_chars=sets.Set('supoercalixaasdseasgoogle asdrf')
      print ''.join(magic_chars&po_chars) #集合的交集

      #判断对象是否是字符串
      isinstance(obj,basestring)

      #字符串对齐
      print '|','hej'.ljust(20),'|','hej'.rjust(20),'|','hej'.center(20),'|'

        

      • xx
    • os.path module
        • os.path.abspath(path) #返回绝对路径
        • os.path.basename(path) #返回文件名
        • os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
        • os.path.dirname(path) #返回文件路径
        • os.path.exists(path)  #路径存在则返回True,路径损坏返回False
        • os.path.lexists  #路径存在则返回True,路径损坏也返回True
        • os.path.expanduser(path)  #把path中包含的"~"和"~user"转换成用户目录
        • os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name”和”${name}”
        • os.path.getatime(path)  #返回最后一次进入此path的时间。
        • os.path.getmtime(path)  #返回在此path下最后一次修改的时间。
        • os.path.getctime(path)  #返回path的大小
        • os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误
        • os.path.isabs(path)  #判断是否为绝对路径
        • os.path.isfile(path)  #判断路径是否为文件
        • os.path.isdir(path)  #判断路径是否为目录
        • os.path.islink(path)  #判断路径是否为链接
        • os.path.ismount(path)  #判断路径是否为挂载点()
        • os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径
        • os.path.normcase(path)  #转换path的大小写和斜杠
        • os.path.normpath(path)  #规范path字符串形式
        • os.path.realpath(path)  #返回path的真实路径
        • os.path.relpath(path[, start])  #从start开始计算相对路径
        • os.path.samefile(path1, path2)  #判断目录或文件是否相同
        • os.path.sameopenfile(fp1, fp2)  #判断fp1和fp2是否指向同一文件
        • os.path.samestat(stat1, stat2)  #判断stat tuple stat1和stat2是否指向同一个文件
        • os.path.split(path)  #把路径分割成dirname和basename,返回一个元组
        • os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组
        • os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组
        • os.path.splitunc(path)  #把路径分割为加载点与文件
        • os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有
        • 3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有
        • 文件名,args则为walk的第三个参数os.path.supports_unicode_filenames  #设置是否支持unicode路径名
      #!/usr/bin/python
      # -*- coding: UTF-8 -*-
       
      import os
      import time
       
      print( os.path.basename('/root/runoob.txt') )   # 返回文件名
      print( os.path.dirname('/root/runoob.txt') )    # 返回目录路径
      print( os.path.split('/root/runoob.txt') )      # 分割文件名与路径
      print( os.path.join('root','test','runoob.txt') )  # 将目录和文件名合成一个路径
      
      file='/root/runoob.txt' # 文件路径
       
      print( os.path.getatime(file) )   # 输出最近访问时间
      print( os.path.getctime(file) )   # 输出文件创建时间
      print( os.path.getmtime(file) )   # 输出最近修改时间
      print( time.gmtime(os.path.getmtime(file)) )  # 以struct_time形式输出最近修改时间
      print( os.path.getsize(file) )   # 输出文件大小(字节为单位)
      print( os.path.abspath(file) )   # 输出绝对路径
      print( os.path.normpath(file) )  # 规范path字符串形式
  • 相关阅读:
    还原 | revert (Cascading & Inheritance)
    过滤器 | filter (Filter Effects)
    过渡时间 | transition-duration (Animations & Transitions)
    过渡延时 | transition-delay (Animations & Transitions)
    过渡属性 | transition-property (Animations & Transitions)
    过渡定时功能 | transition-timing-function (Animations & Transitions)
    过渡 | transition (Animations & Transitions)
    ProxySQL 读写分离
    《抛弃learning rate decay吧!》
    《Tensorflow 中 learning rate decay 的奇技淫巧 》
  • 原文地址:https://www.cnblogs.com/tben/p/10895047.html
Copyright © 2011-2022 走看看