Skip to content Skip to sidebar Skip to footer

Attributeerror: 'module' Object Has No Attribute 'open'

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_2018-03-02/BBG.XTKS

Solution 1:

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

import lzma  # python 3, try lzmaffi in python 2withopen('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

Solution 2:

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.

defopen_lzma_file(f, *args, **kwargs):
    import os
    try:
        import lzma
    except ImportError:
        raise NotImplementedError('''This version of python doesn't have "lzma" module''')
    ifhasattr(lzma, 'open'):
        # Python 3.3+# lzma.open supports 'str', 'bytes' and file-like objectreturn lzma.open(f, *args, **kwargs)
    # Python 2.7.x# This version has LZMAFile # LZMAFile doesn't take-file like object in Python 2.7ifnotisinstance(f, basestring):
        # probably a file like objectifhasattr(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.

withopen_lzma_file(myfile,'rt') as f:
   pair_info=pd.read_csv(f,engine='c',header=0,index_col=0)

Post a Comment for "Attributeerror: 'module' Object Has No Attribute 'open'"