1.Python format 格式化函数,format 函数可以接受不限个参数,位置可以不按顺序。
简单举例:更多参考http://www.runoob.com/python/att-string-format.html
2.Python 字典(Dictionary) keys()方法,用dict.keys()方法返回一个字典dict的所有键。
test = {
"statusCode": 200,
"data": {
"totoal": "5",
"height": "5.97",
"weight": "10.30",
"age": "11"
},
"msg": "成功"
}
def get_keyvalue(data):
for key in data.keys():
key_value = data.get(key)
if isinstance(key_value, dict):
get_keyvalue(key_value)
else:
print('key='+str(key)+' '+'value='+str(key_value))
print('直接使用data.keys()方法取出key'+str(test.keys()))
print('使用递归循环取出data.keys')
get_keyvalue(test)
3.Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
4.Python isdigit()方法。判断字符串中的字符是否只由数字组成,如果是的话返回True,如果否返回False
5.Python isinstance(str, type)用法。
判断一个对象是否是一个已知的类型isinstance (a,int);isinstance (a,str);isinstance (a,(str,int,list));isinstance (a,dict)等
6.python split()的用法,将字符串根据某个字符进行分割。
test = "aaa/bbbb"
print(test.split('/')[0])
print(test.split('/')[1])
7.python join()的用法,将序列中的元素以指定的字符连接生成一个新的字符串。
str = "-"
seq = ("a", "b", "c") # 字符串序列
print(str.join(seq))
list = ['1', '2', '3', '4', '5']
print(''.join(list))
结果:
a-b-c
12345
8.Python ljust() 方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
str1 = "this is string example....wow!!!"
print(str1.ljust(50, '*'))
结果:this is string example....wow!!!******************
9.Python floor() 函数,floor() 返回数字的下舍整数。floor()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。
# 向左取整
math.floor(-45.17)
math.floor(100.12)
结果-46 100
10.Python max() 函数,max() 方法返回给定参数的最大值,参数可以为序列、字符串、列表中包含的元组。
11.Python 内置函数sorted()排序,想要逆序将可选参数reverse
设置为True。
>>>sorted((1,4,8,9,3,6), reverse=True)
Out[36]: [9, 8, 6, 4, 3, 1]
12.Python strip()方法,方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
str = "11121323abcrunoob313211"
print(str.strip('12')) # 传两个字符12,会拆分,1,2,最后跟21结果一样
13.判断一个字符或者字符串是否在另一个字符串中存在的几种方法
使用in,例如 if 'i' in 'instar'
使用count,例如 if 'instar'.count('i')>0 #返回字符串'i'存在的个数
使用find,例如 if 'instar'.find('i')!=-1 #因为find是找到的第一个匹配的索引,如果有多个,只返回第一个的索引,找不到返回-1
14.Python replace()方法,返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
15. os.listdir(path)将path目录下的所有文件(不含文件夹)输出到list中
16.os.path.isdir('cookie.txt')判断cookie.txt是否为一个目录,os.path.isfile()用于判断对象是否为一个文件
17.numpy库的unique函数,去除一维数组或者列表中重复的元素,并按元素由大到小返回一个新元组或者列表
18.arr = [1,3,5] arr.sort()排序print(arr)
19.datetime.datetime.strftime(时间time, '%Y-%m-%d %H:%M:%S')
20.使用Python写入Excel之后,表格打开报格式错误。解决方法:把表格后缀修改为.xls,xls为兼容模式
21.str[::-1] #创造一个与原字符串顺序相反的字符串
22.list的加法合并排序:a = [1,5,7,9] b = [2, 2, 6 ,8] x = list(a+b) x.sort() print(x)
23.dict的遍历:①通过dict.keys,for key in dict.keys(): value = dict.get(key)②for key, value in dict.items(): print(key,value)
24.dict的合并:dict1={},dict2={},new_dict = dict(dict1,**dict2)
25.dict的排序
26.range的简单应用:从哪个数开始到哪个数结束,步长多少。默认从0开始
27.urlencode()。传入参数类型:字典;功能:将存入的字典参数编码为URL查询字符串,即转换成以key1=value1&key2=value2的形式;导入:from urllib.parse import urlencode
28.python中datetime中now和utcnow。now本地时间,utcnow就是utc时间。咱们国家属于东八区,时间比标准时间早8个小时,那就是now时间-8