
正文
python如何存储函数 python存储并导入函数模块
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python如何执行存储过程,获取存储过程返回值
可以使用如下方法:存储过程定义基本如下:ALTER procedure [dbo]. [mysp] @Station varchar( 50), @SN varchar( 50), @Info varchar( 500)output, @Msg varchar( 500)output 1. 使用adodbapiPython代码 from adodbapi import connect server = 'dbserver' user = 'username' password = 'password' database = 'database' sp = 'sp' station = 'station' sn = 'sn' try : db = connect('Provider=SQLOLEDB.1 ;Data Source=%s;Initial Catalog=%s;/ User ID=%s;Password=%s;'%(server, database, user, password)) except Exception, e: print e else : cur = db.cursor() msg = cur.callproc(sp, (station, sn)) #参数与存储过程有关 if len(msg) 1 : if msg[-1 ] is None : print 'sn is ok. Can be test at this station' else : print msg[-1 ] finally : try : db.close() except : pass 2. 使用pymssqlPython代码 from pymssql import connect server = 'dbserver' user = 'user' password = 'password' database = 'database' sp = 'sp' station = 'station' sn = 'sn' sql = ['set nocount on' ] sql.append('declare @Msg varchar(500)' ) sql.append('declare @return_value varchar' ) sql.append("exec @return_value = %s @Station = '%s', @SN = '%s', @Info = '@Info', @Msg = @Msg output" ) sql.append('select @Msg, @return_value' ) sql = '/n' .join(sql) % (sp, station, sn) def ffchk(server, user, password, database, sql): try : db = connect(host = server, database = database, user = user, password = password, login_timeout = 10 ) cur = db.cursor() cur.execute(sql) except Exception, e: print e else : cur.nextset() # 要加上这句才能通过fetch函数取到值 注意:这语句在Python2.7对应python如何存储函数的pymssql版本中是错误python如何存储函数!python如何存储函数!python如何存储函数!
相关问答
Q1: python如何导入函数
python的内建函数即是python自带的函数,这种函数不需要定义,并且不同的内建函数具有不同的功能,可以直接使用。
以下是部分内建函数用法及说明
1、abs(),返回数字的绝对值。
2、all(),如果集合中所有元素是true或集合为空集合,返回True。
3、any(),如果集合中有一项元素是true,返回True;空集合为False
4、ascii(), 返回一个表示对象的字符串。
5、bin(),将整数转换为前缀为“0b”的二进制字符串。
6、bool(),返回一个布尔值,即True或者之一False。
7、bytearray(),返回一个新的字节数组。
8、callable(对象)判断对象参数是否可被调用(可被调用指的是对象能否使用()括号的方法调用)
9、chr(),返回表示Unicode代码点为整数i的字符的字符串。与ord()函数相反。
推荐学习《python教程》
10、classmethod,将方法转换为类方法。
11、compile,将源代码编译为代码或AST对象。代码对象可以由exec()或执行eval()。 source可以是普通字符串,字节字符串或AST对象。
12、dic(),创建一个字典
13、divmod(a,b),将两个数作为参数,并在使用整数除法时返回由商和余数组成的一对数
14、enumerate(iterable,start = 0)
enumerate是枚举、列举的意思
对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值
enumerate多用于在for循环中得到计数
15、eval,将一个字符串变为字典
16、exec(object [, globals[, locals]])exec语句用来执行储存在字符串或文件中的Python语句
17、filter(功能,可迭代)
filter函数用于过滤序列
filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
Q2: 如何在python中读写和存储matlab的数据文件
使用sicpy.io即可.sicpy.io提供python如何存储函数了两个函数loadmat和savemat,非常方便.
以前也有一些开源的库(pymat和pymat2等)来做这个事,
不过自从有python如何存储函数了numpy和scipy以后,这些库都被抛弃python如何存储函数了.
下面是一个简单的测试程序,具体的函数用法可以看帮助文档:
[python] view plaincopy在CODE上查看代码片派生到python如何存储函数我的代码片
import scipy.io as sio
import matplotlib.pyplot as plt
import numpy as np
#matlab文件名
matfn=u'E:/python/测试程序/162250671_162251656_1244.mat'
data=sio.loadmat(matfn)
plt.close('all')
xi=data['xi']
yi=data['yi']
ui=data['ui']
vi=data['vi']
plt.figure(1)
plt.quiver( xi[::5,::5],yi[::5,::5],ui[::5,::5],vi[::5,::5])
plt.figure(2)
plt.contourf(xi,yi,ui)
plt.show()
sio.savemat('saveddata.mat', {'xi': xi,'yi': yi,'ui': ui,'vi': vi})
Q3: python如何让用户直接输入作为实参保存到函数里
形参就是函数入口的参数python如何存储函数,函数入口参数传递只有传值与传值两种区别。传值在python里就是以对象python如何存储函数,比如数组或者是类来传递。至于实参,我印象中是传递常量吧。如果不是这样,应该没有实参的说法。是某些老师为了忽悠,编造出来的概念游戏。简单变量应该是指相对对象来讲的。在python里,只有对象与基本变量类型。简单变量的说法在python里似乎也没有意义。所以,忘记简单变量与实参这样的说法。
Q4: 如何将python运行结果保存成txt?
将python运行结果保存成txt的具体操作步骤如下:
1、首先我们打开电脑桌面,在电脑桌面上点按win+R进入运行,在搜索框里输入cmd并点击确定。
2、然后我们找到图示选项确认查看一下使用的python软件是否已经安装numpy模块。
3、然后我们可以打开python软件输入代码查看关于save函数的使用语法及其实例。
4、如图所示为关于savetxt函数的使用语法及其实例。
5、如图所示为生成的一个数据如何保存为txt格式文件里的代码。
6、输入代码运行然后我们就可以将python运行结果保存成txt了。
Q5: python 字典可以储存函数吗
Python中是没有switchpython如何存储函数的, 所以有时python如何存储函数我们需要用switchpython如何存储函数的用法, 就只能通过if else来实现python如何存储函数了. 但if else写起来比较冗长,
这时就可以使用Python中的dict来实现, 比switch还要简洁. 用法如下:
如果是key1的情况就执行func1, 如果是key2的情况就执行func2...(func1, func2...所有的函数的参数形式需要相同),
假设各个函数参数均为(arg1, arg2):
dictName = {"key1":func1, "key2":func2, "key3":func3"...}#字典的值直接是函数的名字,不能加引号dictName[key](arg1, arg2)
示例代码如下:
#!/usr/bin/python#File: switchDict.py#Author: lxw#Time: 2014/10/05import redef add(x, y): return x + ydef sub(x, y): return x - ydef mul(x, y): return x * ydef div(x, y): return x / ydef main():
inStr = raw_input("Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.\n")
inList = re.split("(\W+)", inStr)
inList[1] = inList[1].strip() print("-------------------------") print(inList) print("-------------------------") #Method 1:
if inList[1] == "+": print(add(int(inList[0]), int(inList[2]))) elif inList[1] == "-": print(sub(int(inList[0]), int(inList[2]))) elif inList[1] == "*": print(mul(int(inList[0]), int(inList[2]))) elif inList[1] == "/": print(div(int(inList[0]), int(inList[2]))) else: pass
#Method 2:
try:
operator = {"+":add, "-":sub, "*":mul, "/":div} print(operator[inList[1]](int(inList[0]), int(inList[2]))) except KeyError: passif __name__ == '__main__':
main()
Output:
PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 + 2
-------------------------['1', '+', '2']-------------------------
3
3PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.4 - 9
-------------------------['4', '-', '9']-------------------------
-5
-5PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.6 / 5
-------------------------['6', '/', '5']-------------------------
1
1PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 9 9
-------------------------['1', '', '9', ' ', '9']-------------------------PS J:\ python .\switchDict.py
Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 ( 9
-------------------------['1', '(', '9']-------------------------PS J:\
个人感觉, 如果想用switch来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有
相同的参数), 就可以用这种方法来实现.
python如何存储函数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python存储并导入函数模块、python如何存储函数的信息别忘了在本站进行查找喔。







