【问题标题】:Reading sas7bdat as pandas dataframe from zipfile从 zipfile 读取 sas7bdat 作为 pandas 数据帧
【发布时间】:2019-08-28 04:17:09
【问题描述】:

我有一个名为 myfile.zip 的 zip 文件,其中包含一个文件 mysasfile.sas7bdat,我想将其读取为 pandas 数据框。我尝试了一些没有用的方法,但这是我目前的方法:

import zipfile

zipfile = zipfile.ZipFile('myfile.zip', 'r')
sasfile = zipfile.open('mysasfile.sas7bdat')

df = pd.read_sas(sasfile)

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-6d55436287b5> in <module>()
      3 imgfile = archive.open('curated_dataset_preview.sas7bdat')
      4 
----> 5 df = pd.read_sas(imgfile)

/opt/python/python35/lib/python3.5/site-packages/pandas/io/sas/sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
     38         filepath_or_buffer = _stringify_path(filepath_or_buffer)
     39         if not isinstance(filepath_or_buffer, compat.string_types):
---> 40             raise ValueError(buffer_error_msg)
     41         try:
     42             fname = filepath_or_buffer.lower()

ValueError: If this is a buffer object rather than a string name, you must specify a format string

【问题讨论】:

    标签: python python-3.x pandas sas


    【解决方案1】:

    您缺少参数format

    import zipfile
    
    zipfile = zipfile.ZipFile('myfile.zip', 'r')
    sasfile = zipfile.open('mysasfile.sas7bdat')
    
    df = pd.read_sas(sasfile, format='sas7bdat')
    

    【讨论】:

    • 我现在在 df = pd.read_sas(sasfile, format='sas7bdat') 上遇到一个新错误
    • 错误是raise ValueError('unknown SAS format')UnsupportedOperation: seek
    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    相关资源
    最近更新 更多