去除空格
去除首尾空格:strip()
去除左边空格:lstrip()
去除右边空格:rstrip()
分割字符串 split()函数
import re
character = " ls, ll la; ll"
cha_list = re.split('[,;\s]',character)
组合2个lst成为字典
dict(zip(lst1,lst2))
有序字典
form collections import OrderedDict
dict_order = collections.OrderedDict()
读取json时保持原有顺序
import json
from collections import OrderedDict
data = json.load(text, object_pairs_hook=OrderedDict)
计算字符数 count()
str = “helloworld,welcome!”
e_count = str.count(“e”)
获取当前时间
import datetime
nowtime=datetime.datetime.now()
nowtime.strftime("%Y%M%D %H:%M:%S")
学习链接
快速转化字符串为列表 eval()
text = “[1,2,4]”
text_lst = eval(text)
replace()替换字符
text = “hello”
text1 = text.replace(“h”, “y”)
注意:replace()不会改变原字符的内容