string.Template
不需要考虑参数的字符串置换,与%操作符以及format方法不同,string.Template方法直接将参数转换为字符串格式,然后将转换后的字符串直接插入结果中去。
https://www.cnblogs.com/paulwhw/p/9231884.html
time.strftime()
时间的字符串替换:
a=time.strftime("%Y年%M月%d日", time.localtime())
time.localtime()
格式化时间戳为本地的时间
import time
t=string.Template("""浙传咨讯:${date},${company_name1}""")
values = {
'date':time.strftime("%Y年%M月%d日", time.localtime()),
'company_name1':sheet['B2'].value,
}
print(t.safe_substitute(values))
清除print之后的自动换行:
在print目标后加“,end=''print(a,end='')
if的判断格式:
![]()
数据是字符串但需要判断是>0,还是<0怎么办?
用int不行,因为有%,所以用string的分割方法可以把%先去掉,%号分割后 会输出为数组,所以数据一定要写成a[0],再用float转一下 就可以判断是否>0
(a=0.44%)因为%后没有东西,所以a[1]是个空串。
![]()