首页 > 技术文章 > python3-day3(内置函数)

weibiao 2016-02-14 18:20 原文

1.内置函数
  1>print(bytearray('王',encoding='utf8'))
  2>print(bytes('王',encoding='utf8'))
  3>bool('')
  4>all(["",None,{},[]])

  5>f= lambda a: a+1
  6>callable(f) #可调用,
  7>chr(97)
  8>ord('a')
  9>#compile()
  10>#enumerate
    li=['alex','tom','lily']
    for i,item in enumerate(li,1):
    print(i,item)

  11>#eval
    memory="6*8"
    print(eval(memory))
  12>#map
    li=[11,22,33,44]
    def func(x):
      return x + 100
    new_li=map(func,li)
    print(list(new_li))
  13>#filter
    li=[11,22,33,44]
    def f(x):
      if x > 33:
        return True
      else:
        return False
    n=filter(f,li)
    print(list(n))
2.官方
https://docs.python.org/3/library/functions.html#next

 

 Built-in Functions  
abs() dict() help() min() setattr()
all() dir() hex() next() slice()
any() divmod() id() object() sorted()
ascii() enumerate() input() oct() staticmethod()
bin() eval() int() open() str()
bool() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()  
delattr() hash() memoryview() set()  

3.open函数
  f=open('test.log','r',encoding='utf8')
  f.seek #指定指针的位置
  f.tell  #查看当前指针的位置
  f.read  #读取文件
  f.read(5)#读取前5个字符
  f.readline #读取一行
  f.readlines#读取所有行
  f.close
  备注:Python3读取按字符,seek位移按字节(位)

打开文件的模式有:

  • r,只读模式(默认)。
  • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
  • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

"+" 表示可以同时读写某个文件

  • r+,可读写文件。【可读;可写;可追加】
  • w+,写读
  • a+,同a

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

  • rU
  • r+U

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

  • rb
  • wb
  • ab

推荐阅读