博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python记录的错误与知识
阅读量:4027 次
发布时间:2019-05-24

本文共 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)
你可能感兴趣的文章
[LeetCode By Python]107. Binary Tree Level Order Traversal II
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>
[leetCode By Python]111. Minimum Depth of Binary Tree
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>
[LeetCode By Python]167. Two Sum II - Input array is sorted
查看>>
[LeetCode BY Python]169. Majority Element
查看>>
[LeetCode By Python]172. Factorial Trailing Zeroes
查看>>
[LeetCode By MYSQL] Combine Two Tables
查看>>
python jieba分词模块的基本用法
查看>>
[CCF BY C++]2017.12 最小差值
查看>>
[CCF BY C++]2017-12 游戏
查看>>
如何打开ipynb文件
查看>>
[Leetcode BY python ]190. Reverse Bits
查看>>
面试---刷牛客算法题
查看>>
Android下调用收发短信邮件等(转载)
查看>>
Android中电池信息(Battery information)的取得
查看>>