zoukankan      html  css  js  c++  java
  • Unicode 和utf-8的转换以及深拷贝和浅拷贝的区别

    必须掌握的点

    一、

    (内存)Unicode二进制字符>>>>>>编码(encode)>>>>>>(硬盘)utf-8二进制字符

    (硬盘)utf-8二进制字符>>>>>>解码(decode)>>>>>>(内存) unicode二进制字符

    二、

    用什么编码存的数据,就用什么编码取

    我们中国这边的操作系统

    windows默认的是GBK

    pycharm默认的是utf-8

    文件头:#coding:utf-8

    python2:默认的字符编码是ASCII码(因为当时的Unicode还没盛行)

    python3:默认的字符编码是utf-8

    现在的计算机:

    内存都是:Unicode

    硬盘都是:utf-8

    文件头:

    #coding:utf-8

    用英文字符,是为了让所有的计算机都可以识别。

    深浅拷贝

    l1 = [1,'a',[4,5,6]]
    print(l1)
    print(id(l1))

    l2 = l1.copy()
    l2[2][1] = 555
    print(l2)
    print(id(l2))

    print(id(l1[2][1]))
    print(id(l2[2][1]))



    import copy

    l1 = [257,"a",[4,5,6]]
    print(l1)
    print(id(l1))

    print(id(l1[2][1]))

    l2 = copy.deepcopy(l1)
    l2[0]= 1000
    print(id(l2))

    print(id(l2[2][1]))

    深拷贝,需要import一个copy的库,这样才可以进行深拷贝。



    1、什么是文件?
    操作系统提供给你操作硬盘的一个工具

    2、为什么要用文件?
    因为人类和计算机要永久保存数据

    3、怎么用文件
    相对路径:a.txt #必须与当前py文件在同一级目录
    绝对路径(举例)D:项目路径pythonxx期dayxxa.txt

    f = open("a.txt")
    print(f.readable())     #判断当前文件是否可读
    print(f.writable()) #判断当前文件是否可写

    del f #回收变量资源
    f.close() #回收操作系统的资源

    举例
    with open(“a.txt”,mode="r")as rf,
    open("a.txt",mode="w")as wf: #with会自动帮你回收操作系统的资源,无需自己操作
    print(rf.readable())
    print(wf.writable())

    r进行转义
    with open(r'D:项目路径pythonxx期dayxxa.txt')as f:
    print(f.read()) #读取文件



  • 相关阅读:
    vmware ubuntu 异常关机无法连接到网络
    Speed up GCC link
    常用的一些解压命令
    Log4j 漏洞复现
    Test Case Design method Boundary value analysis and Equivalence partitioning
    CCA (Citrix Certified Administrator) exam of “Implementing Citrix XenDesktop 4”
    What is Key Word driven Testing?
    SAP AGS面试小结
    腾讯2013终端实习生一面
    指针的引用
  • 原文地址:https://www.cnblogs.com/medigrat/p/11814402.html
Copyright © 2011-2022 走看看