python读取文件时提示"UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 205: illegal multibyte sequence"
解决办法1.
FILE_OBJECT= open('order.log','r', encoding='UTF-8')
解决办法2.
FILE_OBJECT= open('order.log','rb')
map排序:
1.按key排序:
items=dict.items()
items.sort()
sorted(dict.items(),key=lambda x:x[0],reverse=False)
2.按value排序
sorted(dict.items(),key=lambda x:x[1],reverse=False)
(ps:在python2.x中还是有cmp函数的,在3.x中已经没有了,但是引入了
import operator #首先要导入运算符模块
operator.gt(1,2) #意思是greater than(大于)
operator.ge(1,2) #意思是greater and equal(大于等于)
operator.eq(1,2) #意思是equal(等于)
operator.le(1,2) #意思是less and equal(小于等于)
operator.lt(1,2) #意思是less than(小于)
)
map取最大最小值:
方法一:
max(dict,key=dict.get)
min(dict,key=dict.get)
方法二:
min(d.items(), key=lambda x: x[1])
min(d.items(), key=lambda x: x[1][0]
min(d.items(), key=lambda x: x[1])[1]
本文解决Python读取文件时的UnicodeDecodeError问题,介绍使用UTF-8编码和二进制模式读取的方法。同时,分享字典排序(按key和value)、取最大最小值的实用技巧。
5842

被折叠的 条评论
为什么被折叠?



