python - 怎么写才合适才优雅
本文介绍了python - 怎么写才合适才优雅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
先上代码
try:
res+="会话数<span style='color: blue;'> "+str(info[1]).strip('\n')+"</span><br>"
except Exception,e:
print e
try:
res+="失效数<span style='color: blue;'> "+str(info[2]).strip('\n')+"</span><br>"
except Exception,e:
print e
try:
res+="连接数<span style='color: blue;'> "+str(info[3]).strip('\n').strip('\t')+"</span><br>"
except Exception,e:
print e
上面的info[1]、info2[2]、info3[3],可能并不存在,所以我用try包起来,以免程序中途停止。而且各个的处理方式不一样。这段代码要怎么写才合适才优雅?
为什么用优雅语言写出来的还是一坨......
解决方案
_list = ('会话数', '失效数', '连接数')
for index, c in enumerate(_list):
try:
res+="{}<span style='color: blue;'> ".format(c) + str(info[index + 1]).strip('\n')"</span><br>"
except Exception,e:
print e
这篇关于python - 怎么写才合适才优雅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文