python消除print的换行符、引用模块和异常处理

1.使用","来消除print输出的默认换行符,如:
def query(cur):
	row = cur.fetchone()
	set_len = len(row)
	while row:
		for i in range(0,set_len): #Note:range is from 0 to set_len-1
			print row[i],      #Note:use ',' to reduce the endl by print
		print ""
		row = cur.fetchone()

2.在引用另一个模块,并且要使用另外一个模块中的成员的时候,类、方法等,必须前面加上模块名,
如在my_class模块中有一个my_class类,现在引用进来并且使用my_class类来创建一个对象的时候:
cluster = my_class.my_class() #remember to add the module name
Python导入模块的方法有两种:import module 和 from module import,区别是前者所有导入的东西使用时需加上模块名的

限定,而后者不要。但是一般情况下还是使用第一种方法,这样可以明确某个方法属于哪一个模块,并且可以避免不同模块中

的名字冲突

3.可以在一个函数中抛出异常,然后在使用这个函数的地方捕捉这个异常,如:
def func(cursor,sql):
	try:
		cursor.execute(sql)
		return cursor
	except pyodbc.Error,e:
		#print e.args[0]
		raise e

sql = "select * from xxx"
try:
	func(cursor,sql)
	print "unexpectly comes here!"
except pyodbc.Error,e:
	print e.args[0]
	print e.args[1]
finally:
	cursor.close()
	conn.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值