本文共 2261 字,大约阅读时间需要 7 分钟。
list转字符串: string = "".join(list) 去空格: string.strip():去左右空格 string.lstrip():去左边空格 string.rstrip():去右边空格 写文件: codecs:可以指定编码 file= codecs.open("text.txt","w",encoding="utf-8") 最快的去重:list TypeError: can't concat bytes to str:https://www.cnblogs.com/fh-fendou/p/8310425.html import math #向上取整 print "math.ceil---" print "math.ceil(2.3) => ", math.ceil(2.3) print "math.ceil(2.6) => ", math.ceil(2.6) #向下取整 print "\nmath.floor---" print "math.floor(2.3) => ", math.floor(2.3) print "math.floor(2.6) => ", math.floor(2.6) #四舍五入 print "\nround---" print "round(2.3) => ", round(2.3) print "round(2.6) => ", round(2.6) python相对路径,绝对路径 相对路径: ./文件名 如果在一个包下demo包 .demo/文件夹/文件名 绝对路径 os.path.abspath("文件名"):查找 获取一个列表中值的索引: list = [Ture,False,'b'] list.index(Ture) -> 0 list.index('b') -> 2 a[::-1] : 倒序排序存入a列表中 %f 格式化定点数,可指定精度 %e 科学计数法计数 %g 根据值的大小采用%e或%f,但最多保留6位有效数字 range(0,5,2):0-5不包括5,每隔俩个-->即(0,2,4) zip()函数的使用: a = [1,2,3] b = [3,4,5] zip(a,b) ->[(1,2),(2,4),(3,5)] for i in zip(range(0,100,2),range(2,101,2)): print(i) 结果:(0, 2) (2, 4) (4, 6)... 文件操作: def getEncodings(): known_face_encodings=[] known_face_names=[] if os.path.exists("images"): #是 否存在该目录 test = os.listdir("images") #列出文件目录名列表 #print("人名列表:",test) for i in range(0,len(test)): path = os.path.join("images",test[i]) #组成路径 #print("%s照片路径%s"%(test[i],path)) if os.path.isdir(path): son_file = os.listdir(path) #print("%s照片内容%s"%(path,son_file)) for j in range(0,len(son_file)): path_son = os.path.join(path,son_file[j]) #print("%s照片路径:%s"%(son_file[j],path_son)) image_array= face_recognition.load_image_file(path_son) enconding = face_recognition.face_encodings(image_array)[0] known_face_encodings.append(enconding) known_face_names.append(test[i]) return known_face_encodings,known_face_names root,dires,files = os.walk("/images") 返回根目录,子文件,文件 Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+ https://blog.csdn.net/zengxyuyu/article/details/53038763 https://blog.csdn.net/u013555719/article/details/77991010 解决方式:打开方式为:wb ----> open("a.txt","wb") python3与python2中urllib使用 :https://blog.csdn.net/IMW_MG/article/details/78555375 Python3中也有urllib和urllib3两个库,其中urllib几乎是Python2中urllib和urllib2两个模块的集合,所以我们最常用的urllib模块,而urllib3则作为一个拓展模块使用。 urlencode方法所在位置 urllib.parse.urlencode(values)