zoukankan      html  css  js  c++  java
  • python复习1

    1:#r模式,在文件不存在时,不会创建新的文件

      f = open('a.txt','r',encoding = 'utf-8')

    2:字符编码

      什么是字符编码?

        把人类的字符翻译成计算机能认识的数字

      什么是字符编码表?

        ASCII

        GBK

        UTF-8

        Unicode

    unicode-------->encode('utf-8')-------------->bytes

    bytes------------>decode('utf-8')--------------->unicode

    原则:

      字符以什么格式编译的,就要以什么格式解码

    ps:

      python3中的字符分为两种

      x='egon'存成unicode

      y=x.encode('utf-8')存成bytes

    python2中的字符串也分成两种

       x = u'egon' 与python3中的字符串相同

      y='alex'与python3中的bytes类型相同

    #r模式的读,在文件不存在时,不会创造新的文件
    f = open('a.txt','r',encoding = 'utf-8')
    print(f.read())
    f.close()


    #b模式即直接从硬盘中读取bytes
    print(f.read().decode('utf-8'))

    #w文本模式的写,文件存在则清空,不存在则创建
    f = open('a.txt','w',encoding = 'utf-8')
    f = open('b.txt','w',encoding = 'utf-8')
    print(f.writeable())#判断是否可写
    print(f.readable())#判断是否可读

    #a文本模式的追加
    f = open('b.txt','a',encoding = 'utf-8')
    print(f.writeable())#写文件涉及到文件光标的移动
    print(f.tell())
    print(f.readline())
    f.write('333 ')
    #r+,w+,a+,代表读的时候可以写,写的时候可以读

    #rb模式即直接从硬盘中读取bytes
    f = open('a.txt','rb')
    print(f.read().decode('utf-8'))

    #wb模式
    f = open('a.txt','wb')
    f.write('你好啊'.encode('utf-8'))


    ##b模式可以读取任何模式的文件
    with open('test.jpg','rb') as read_f,
    open('test1.jpg','wb') as write_f:
    for line in read_f:
    write_f.write(line)
  • 相关阅读:
    ios-app提交审核问题总结
    mui混合app请求过程处理(缓存、加载、刷新机制)
    vue引入assets和static静态资源问题
    mui入门教程
    scroll.js
    jQuery.Running.js
    CSS 编码技巧
    textillate.js
    3. 戏说VHDL之入门游戏一:流水灯
    2. 流水灯小计
  • 原文地址:https://www.cnblogs.com/huangxiaohan/p/7778444.html
Copyright © 2011-2022 走看看