python - 属性错误 : 'module' object has no attribute 'open'

标签 python

我正在尝试使用以下代码在 Linux 中打开压缩为 .lzma 文件的 .csv:

import lzma
import pandas as pd

myfile= '/home/stacey/work/roll_158_oe_2018-03-02/BBG.XTKS.8219.S/inst.BBG.XTKS.8219.S.csv.lzma'

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

其中 myfile 是 Linux 中存在的路径。

但是我得到了错误:

with lzma.open(stock,'rt') as f:
AttributeError: 'module' object has no attribute 'open'

我尝试添加以下内容:

import lzma
import pandas as pd

    myfile= '/home/stacey/work/roll_158_oe_2018-03-02/BBG.XTKS.8219.S/inst.BBG.XTKS.8219.S.csv.lzma'

    with open(myfile) as compressed:
         with lzma.LZMAFile(compressed,'r') as uncompressed:
             line in uncompressed:
             print(line)  

但是我得到了错误:

    with lzma.LZMAFile(compressed,'r') as uncompressed:
TypeError: coercing to Unicode: need string or buffer, file found

我也试过:

import pandas as pd
import lzma
import pickle

myfile= '/home/stacey/work/roll_158_oe_2018-03-02/BBG.XTKS.8219.S/inst.BBG.XTKS.8219.S.csv.lzma'

myoutput = pickle_load(myfile,'lzma')
print(myoutput )

def pickle_load(filePath,compression=None):
    open_cmd=open if compression is None else __import__(compression).open
    with open_cmd(filePath,'r') as f:
        output=pickle.load(f)
    return output

但是我又得到了错误:

open_cmd=open if compression is None else __import__(compression).open
AttributeError: 'module' object has no attribute 'open'

当我在 cmd 行上运行 python-v 时,我得到了输出:

[scoleman@ip-192-168-9-132 port_1m]$ python -v

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib64/python2.7/site.pyc matches /usr/lib64/python2.7/site.py
import site # precompiled from /usr/lib64/python2.7/site.pyc
# /usr/lib64/python2.7/os.pyc matches /usr/lib64/python2.7/os.py
import os # precompiled from /usr/lib64/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib64/python2.7/posixpath.pyc matches /usr/lib64/python2.7/posixpath.py
import posixpath # precompiled from /usr/lib64/python2.7/posixpath.pyc
# /usr/lib64/python2.7/stat.pyc matches /usr/lib64/python2.7/stat.py
import stat # precompiled from /usr/lib64/python2.7/stat.pyc
# /usr/lib64/python2.7/genericpath.pyc matches /usr/lib64/python2.7/genericpath.                              py
import genericpath # precompiled from /usr/lib64/python2.7/genericpath.pyc
# /usr/lib64/python2.7/warnings.pyc matches /usr/lib64/python2.7/warnings.py
import warnings # precompiled from /usr/lib64/python2.7/warnings.pyc
# /usr/lib64/python2.7/linecache.pyc matches /usr/lib64/python2.7/linecache.py
import linecache # precompiled from /usr/lib64/python2.7/linecache.pyc
# /usr/lib64/python2.7/types.pyc matches /usr/lib64/python2.7/types.py
import types # precompiled from /usr/lib64/python2.7/types.pyc
# /usr/lib64/python2.7/UserDict.pyc matches /usr/lib64/python2.7/UserDict.py
import UserDict # precompiled from /usr/lib64/python2.7/UserDict.pyc
# /usr/lib64/python2.7/_abcoll.pyc matches /usr/lib64/python2.7/_abcoll.py
import _abcoll # precompiled from /usr/lib64/python2.7/_abcoll.pyc
# /usr/lib64/python2.7/abc.pyc matches /usr/lib64/python2.7/abc.py
import abc # precompiled from /usr/lib64/python2.7/abc.pyc
# /usr/lib64/python2.7/_weakrefset.pyc matches /usr/lib64/python2.7/_weakrefset.                              py
import _weakrefset # precompiled from /usr/lib64/python2.7/_weakrefset.pyc
import _weakref # builtin
# /usr/lib64/python2.7/copy_reg.pyc matches /usr/lib64/python2.7/copy_reg.py
import copy_reg # precompiled from /usr/lib64/python2.7/copy_reg.pyc
# /usr/lib64/python2.7/traceback.pyc matches /usr/lib64/python2.7/traceback.py
import traceback # precompiled from /usr/lib64/python2.7/traceback.pyc
# /usr/lib64/python2.7/sysconfig.pyc matches /usr/lib64/python2.7/sysconfig.py
import sysconfig # precompiled from /usr/lib64/python2.7/sysconfig.pyc
# /usr/lib64/python2.7/re.pyc matches /usr/lib64/python2.7/re.py
import re # precompiled from /usr/lib64/python2.7/re.pyc
# /usr/lib64/python2.7/sre_compile.pyc matches /usr/lib64/python2.7/sre_compile.                              py
import sre_compile # precompiled from /usr/lib64/python2.7/sre_compile.pyc
import _sre # builtin
# /usr/lib64/python2.7/sre_parse.pyc matches /usr/lib64/python2.7/sre_parse.py
import sre_parse # precompiled from /usr/lib64/python2.7/sre_parse.pyc
# /usr/lib64/python2.7/sre_constants.pyc matches /usr/lib64/python2.7/sre_consta                              nts.py
import sre_constants # precompiled from /usr/lib64/python2.7/sre_constants.pyc
dlopen("/usr/lib64/python2.7/lib-dynload/_localemodule.so", 2);
import _locale # dynamically loaded from /usr/lib64/python2.7/lib-dynload/_local                              emodule.so
# /usr/lib64/python2.7/_sysconfigdata.pyc matches /usr/lib64/python2.7/_sysconfi                              gdata.py
import _sysconfigdata # precompiled from /usr/lib64/python2.7/_sysconfigdata.pyc
import encodings # directory /usr/lib64/python2.7/encodings
# /usr/lib64/python2.7/encodings/__init__.pyc matches /usr/lib64/python2.7/encod                              ings/__init__.py
import encodings # precompiled from /usr/lib64/python2.7/encodings/__init__.pyc
# /usr/lib64/python2.7/codecs.pyc matches /usr/lib64/python2.7/codecs.py
import codecs # precompiled from /usr/lib64/python2.7/codecs.pyc
import _codecs # builtin
# /usr/lib64/python2.7/encodings/aliases.pyc matches /usr/lib64/python2.7/encodi                              ngs/aliases.py
import encodings.aliases # precompiled from /usr/lib64/python2.7/encodings/alias                              es.pyc
# /usr/lib64/python2.7/encodings/utf_8.pyc matches /usr/lib64/python2.7/encoding                              s/utf_8.py
import encodings.utf_8 # precompiled from /usr/lib64/python2.7/encodings/utf_8.p                              yc
Python 2.7.12 (default, Sep  1 2016, 22:14:00)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
dlopen("/usr/lib64/python2.7/lib-dynload/readline.so", 2);
import readline # dynamically loaded from /usr/lib64/python2.7/lib-dynload/readl                              ine.so

当我运行 import lzma 时,我得到了输出:

>>> import lzma
dlopen("/usr/lib64/python2.7/dist-packages/lzma.so", 2);
import lzma # dynamically loaded from /usr/lib64/python2.7/dist-packages/lzma.so

我做错了什么以及如何让它工作?我环顾四周,但看不到任何其他解决方案。

最佳答案

显然你需要从 lzma 模块调用一个类来打开文件:

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)

摘自How to open and read LZMA file in-memory

关于python - 属性错误 : 'module' object has no attribute 'open' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089806/

相关文章:

python - 是否可以在 python 中将动态(带有静态文件)Web 应用程序作为没有其他依赖项的单个包提供服务?

python - 根据默认大小 reshape np.array

python - confluence_kafka消费者偏移计数重置问题

python - 将 PyTorch 张量转换为 python 列表

python - 使用scipy curve_fit拟合指数曲线(拟合曲线确实匹配真实曲线)

python - 当指定父窗口时,PyQt5 窗口未在 show() 上打开

python - 如何获取数据框中的累计行数?

python - 将 String 转换为 Python 3 中 hmac.new() 的 Bytes 对象

python - 从 Cookie 中获取用户名

python - 在Python中分割字典并将其写入不同的csv文件