AttributeError: 'module' object has no attribute 'open'

后端 未结 2 923
北海茫月
北海茫月 2021-01-28 08:52

I am trying to open a .csv compressed to a .lzma file in Linux using the following code:

import lzma
import pandas as pd

myfile= \'/home/stacey/work/roll_158_oe         


        
相关标签:
2条回答
  • 2021-01-28 09:29

    Apparently you need to call a class from the lzma module to open the file:

    import lzma  # python 3, try lzmaffi in python 2
    with open('one-csv-file.xz') as compressed:
        with lzma.LZMAFile(compressed) as uncompressed:
            for line in uncompressed:
                do_stuff_with(line)
    

    Extracted from How to open and read LZMA file in-memory

    0 讨论(0)
  • 2021-01-28 09:40

    There are some differences in lzma module between Python 2.7.x and Python 3.3+.

    Python 2.7.x doesn't have lzma.open and lzma.LZMAFile doesn't take file-like object. Here's a function to open an lzma file Python version independent way.

    def open_lzma_file(f, *args, **kwargs):
        import os
        try:
            import lzma
        except ImportError:
            raise NotImplementedError('''This version of python doesn't have "lzma" module''')
        if hasattr(lzma, 'open'):
            # Python 3.3+
            # lzma.open supports 'str', 'bytes' and file-like object
            return lzma.open(f, *args, **kwargs)
        # Python 2.7.x
        # This version has LZMAFile 
        # LZMAFile doesn't take-file like object in Python 2.7
        if not isinstance(f, basestring):
            # probably a file like object
            if hasattr(f, 'name') and os.path.exists(f.name):
                f = f.name
            else:
                raise TypeError('Expected `str`, `bytes`, `unicode` or file-like object with valid `name` attribute pointing to a valid path')
        return lzma.LZMAFile(f, *args, **kwargs)
    

    Usage;
    Simply pass a str , bytes or file-like object.

    with open_lzma_file(myfile,'rt') as f:
       pair_info=pd.read_csv(f,engine='c',header=0,index_col=0)
    
    0 讨论(0)
提交回复
热议问题