一 有一种情况,在Windows系统上面有一个文件,编码为gbk,将其上传到Linux虚拟机,系统编码为utf-8,
使用cat命令查看时是乱码,这时如何解决?

[root@localhost ~]# cat x.py #!/usr/bin/env python # -*- coding:utf-8 -*- with open('hhh.txt',mode='r') as f1: content=f1.read() #content1=content.decode('gbk')#(u'u4f60u597d', <type 'unicode'>) #content1=content.decode('gbk').encode('utf-8')#('xe4xbdxa0xe5xa5xbd', <type 'str'>) content1=content.decode('gbk').encode('utf-8') print(content1) print(type(content1)) ##结果为: #这个我认为首选 #你好 #<type 'str'> ''' with open('hhh.txt','rb') as f1: content=f1.read() content1=content.decode('gbk') print(content1) print(type(content1)) ###结果为 #你好 #<type 'unicode'> '''
如果想要使用cat命名查看而不报错呢

[root@localhost ~]# cat xx.py #!/usr/bin/env python # -*- coding:utf-8 -*- import os with open('hhh.txt',mode='r') as f1, open('hhh.bak',mode='w') as f2: content=f1.read().decode('gbk').encode('utf-8') f2.write(content) os.remove('hhh.txt') os.rename('hhh.bak','hhh.txt')
二 运算符优先级
算术操作符>比较运算符>赋值运算符(=、-=、+=)>逻辑运算符
算数 + - * / 比较 > < == != 逻辑 not and or
or的用法:遇到真就返回,没有的话就是False
负数,整数都为真,返回数字本身,两数相遇取前值
0和False属于同一级别,它们比较时,谁在后面取谁
and的用法:遇到假返回,没有的话一般为真
两个元素相遇时,遇到0或者False,返回0或False
两真相遇取后者
0和False相遇,谁前取谁
三 编码
utf-8 英文:1 字节 中文 3 个字节
gbk 英文: 1字节 中文 2 字节
四 给一个列表去重,先转化成集合再转回列表 list(set(a))
五深浅copy
1 浅copy第一层各自独立,从第二层开始,共用一个内存地址
2 深copy无论多少层都是独立的
3 两个变量赋值就是同一个内存地址,一个改变,另外一个也会改变
4 通过切片赋值属于浅copy,第一层变化不会影响另外一个,从第二层开始变化会同步,例如:a=b[:]
六:fromkyes
fromkeys(前面,后面)是创建一个字典
前面可以是字符串,则每个字符都是一个键
前面可以是列表,则每个元素都是一个键
前面可以是元组,则每个元素都是一个键
前面可以是字典,则它的键值作为新字典的键
后面是一个整体,作为新字典的值
七列表,字典相加
列表:[1,2,3]+[4,5,6]
字典:print(dict({'1':'a','2':'b'},**{'3':'c','4':'d'}))
八 列表加上多个元素
一个列表往往有很多元素,甚至往往要加上一些元素
列表在后面加上多个元素的方法:
例如:l5=[1,2,3] l5[10:]='a','b','c' #这里的10可以为其他数字,只有大于2就成 print(l5) #结果为[1,2,3,'a','b','c']
九 关于类中的super()
super方法找的是父类的方法,那到底怎么理解呢,是不是找的是当前类的父类?

class Foo: def f1(self): print('foo.f1') super().f2() #super它会按照下面Sub.mro()的顺序去找上一层找到了 <class '__main__.Bar'> #尽管Foo和Bar没有关系,所以super是按照mro的列表查找的 class Bar: def f2(self): print('Bar.f2') class Sub(Foo,Bar): pass s=Sub() print(Sub.mro()) #[<class '__main__.Sub'>, <class '__main__.Foo'>, <class '__main__.Bar'>, <class 'object'>] #mro()是查找当前类的继承顺序 s.f1() #结果为 foo.f1 Bar.f2
十关于可hash对象,什么可以作为字典的key
我们知道要作为字典的key,它必须是不可变得,也就是可hash,有int,float,str,tuple,frozenset
可hash对象它就要求这个对象不能有多个值
例如

>>> a=('a','b',) >>> print(id(a)) 2611973911432 >>> a=('a','b','c') >>> print(id(a)) 2611973840184 #id地址改变了,可以作为hash对象 >>> b=['a','b',] >>> print(id(b)) 2611973903688 >>> print(id(b)) 2611973903688 >>> print(id(b)) 2611973903688 >>> b.append('c') >>> print(id(b)) 2611973903688 #id的值一直没变
那么元组里面嵌套是否可以hash呢

# e=([1,2,3],'b','c') # print(id(e)) #1940510753560 # e[0].append(4) # print(id(e)) #1940510753560 #id值没变 # s={([1,2,3],'b','c'):'a'} # print(hash(s)) #TypeError: unhashable type: 'list'
因此,元组要作为可hash对象,必须是单纯的元组
不可变对象:value的值变了,id的值也跟着一起变
可变对象:在id不变的情况下,value的值可以变
十一:返回一个字典键的列表
s={'a':1,'b':2,'c':3}l=list(s) 扩展,返回一个字典的values值组成的列表 l1=list(s.values())
十二:列表的pop不带索引就是删除最后一个元素
十三:关于新式类和经典类
python2中使用的是经典类,深度优先,可以修改成新式类
python3中默认使用的就是新式类,新式类使用的是广度优先
十三:关于集合set的哈希和不可哈希
set1={1,2,3}
# dic={set1:'a'}
# print(dic) #报错 TypeError: unhashable type: 'set'
set2=frozenset(set1) #集合使用frozenset就可哈希了
print(set2) #frozenset({1, 2, 3})
dic1={set2:'a'}
print(dic1) #{frozenset({1, 2, 3}): 'a'}
十四:关于 modules
Python中所有加载到内存的模块都放在sys.modules。当import一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将模块的名字加入到正在调用import的模块的Local名字空间中。
如果没有加载则从sys.path目录中按照模块名称查找模块文件,模块文件可以是py、pyc、pyd,找到后将模块载入内存,并加入到sys.modules中,并将名称导入到当前的Local名字空间。
import test2 --->对应的内存中找到的是 sys.modules['test2']
from a import test2 ---->对应的内存中 sys.modules['a.test2']
十五 关于__str__和__repr__
我们来看一个例子
class Fruit: def __str__(self): return 'Fruit_str' def __repr__(self): return 'Fruit_repr'
aa=Fruit()
如果我们打印
print(aa) #输出为 Fruit_str
print(repr(aa)) #输出为Fruit_repr
如果我们修改一下呢
class Fruit: # def __str__(self): # return 'Fruit_str' def __repr__(self): return 'Fruit_repr'
bb=Fruit()
print(bb) #输出为 Fruit_repr
总结:单纯print对象的是首先查找 __str__,没有就会找__repr__