简体   繁体   English

使用Python的adodbapi读取sas7bdat文件

[英]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... 另外,要抢占一些替代方法...

  1. I do not want to create *.csv files in SAS. 我不想在SAS中创建* .csv文件。
  2. The computer with Python does not have SAS installed, but does have the Providers for OLE DB installed. 装有Python的计算机未安装SAS,但已安装OLE DB的提供程序。 I know for a fact that the VB code I provided works without SAS in read-only mode. 我知道,我提供的VB代码在只读模式下无需SAS即可工作。 You can download these drivers here: http://support.sas.com/downloads/browse.htm?cat=64 您可以在以下位置下载这些驱动程序: http : //support.sas.com/downloads/browse.htm?cat=64
  3. I am not expert in SAS. 我不是SAS专家。 Honestly, I find their tool cumbersome, confusingly documented, and slow. 老实说,我发现他们的工具笨重,文档混乱且运行缓慢。 I noticed that there are some other products listed called "IOMProvider" and "SAS/SHARE". 我注意到还列出了一些其他产品,称为“ IOMProvider”和“ SAS / SHARE”。 If there's an easier way of doing this using those ADO providers, feel free to document it. 如果有使用这些ADO提供程序的简便方法,请随时对其进行记录。 However, what I'm really looking for is a way of doing this entirely within Python with a relatively simple bit of code. 但是,我真正要寻找的是一种完全在Python中使用相对简单的代码来完成此操作的方法。
  4. Oh, and I'm aware of Python's sas7bdat package, but we're using Python 3.3.5 and it doesn't seem to be compatible. 哦,我知道Python的sas7bdat软件包,但我们使用的是Python 3.3.5,它似乎不兼容。 Also, I couldn't figure out how to use it on 2.7 anyways as there's not a lot of documentation and even a question on how to use the tool, which, to this day, is unanswered. 另外,我仍然不知道如何在2.7上使用它,因为没有太多的文档,甚至还没有关于如何使用该工具的问题,至今仍未得到解决。 Python sas7bdat module usage Python sas7bdat模块用法

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM