[英]Reading sas7bdat files with Python's adodbapi
I'm trying to read a sas7bdat file from SAS (product of the SAS Institute) into Python. 我正在尝试将SAS(SAS Institute的产品)中的sas7bdat文件读取到Python中。
Yes, I'm a aware that we could export to *.csv files, but I'm trying to avoid that as that will double the number of files we need to create. 是的,我知道我们可以导出到* .csv文件,但是我试图避免这样做,因为这会使我们需要创建的文件数量增加一倍。
There's good documentation for doing this in Visual Basic. 在Visual Basic中有很好的文档可以做到这一点。 Still, I want it in Python.
尽管如此,我还是希望在Python中使用它。 For example, in VB you could write...
例如,在VB中,您可以编写...
Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
obConnection.Provider = "sas.LocalProvider"
obConnection.Properties("Data Source") = "c:\MySasData"
obConnection.Open
rs.Open "work.a", cn, adOpenStatic, adLockReadOnly, adCmdTableDirect
To open your dataset. 打开数据集。
But I can't crack the nut to make this work in python. 但是我不能轻易使它在python中工作。
I can type... 我可以打字...
import adodbapi
cnstr = 'Provider=sas.LocalProvider;c:\\MySasData'
cn = adodbap.connect(cnstr)
And a can get a cursor... 一个可以得到一个游标...
cur = cn.cur()
But beyond that, I'm stumped. 但是除此之外,我很沮丧。 I did find a cur.rs, which sounds like a recordset, but it is an object with a type of None.
我确实找到了一个cur.rs,听起来像一个记录集,但这是一个类型为None的对象。
Also, to preempt some alternative methods... 另外,要抢占一些替代方法...
Thanks! 谢谢!
Didn't test it with SAS as I don't have a provider installed currently, it should go like this: 由于当前未安装提供程序,因此未使用SAS测试,它应如下所示:
cn = adodbapi.connect(cnstr)
# print table names in current db
for table in cn.get_table_names():
print(table)
with cn.cursor() as c:
#run an SQL statement on the cursor
sql = 'select * from your_table'
c.execute(sql)
#get the results
db = c.fetchmany(5)
#print them
for rec in db:
print(rec)
cn.close()
EDIT: Just found this http://support.sas.com/kb/30/795.html so you might need to use other provider for this method, have a look at IOM privoder ( https://www.connectionstrings.com/sas-iom-provider/ , http://support.sas.com/documentation/tools/oledb/gs_iom_tasks.htm ) 编辑:刚发现此http://support.sas.com/kb/30/795.html,所以您可能需要使用其他提供程序进行此方法,请看一下IOM privoder( https://www.connectionstrings.com / SAS-IOM提供商/ , http://support.sas.com/documentation/tools/oledb/gs_iom_tasks.htm )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.