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)
  • 相关阅读:
    html表单的创建
    mysql数据库连接标准操作
    关于Apache+MySQL+PHP下载及配置注意事项
    两个范例
    Stack类
    Collections类集
    key可以重复的map集合:IdentityHashMap
    foreach对集合的输出作用
    ListIterator接口
    【官方方法】xcode7免证书真机调试
  • 原文地址:https://www.cnblogs.com/huangxiaohan/p/7778444.html
Copyright © 2011-2022 走看看