Python 属性错误:';模块';对象没有属性';打开';
我正在尝试使用以下代码在Linux中打开压缩为.lzma文件的.csv文件:Python 属性错误:';模块';对象没有属性';打开';,python,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
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'
with lzma.LZMAFile(compressed,'r') as uncompressed:
TypeError: coercing to Unicode: need string or buffer, file found
open_cmd=open if compression is None else __import__(compression).open
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.open(stock,'rt') as f:
AttributeError: 'module' object has no attribute 'open'
with lzma.LZMAFile(compressed,'r') as uncompressed:
TypeError: coercing to Unicode: need string or buffer, file found
open_cmd=open if compression is None else __import__(compression).open
AttributeError: 'module' object has no attribute 'open'
我也尝试过:
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
但我还是犯了一个错误:
with lzma.open(stock,'rt') as f:
AttributeError: 'module' object has no attribute 'open'
with lzma.LZMAFile(compressed,'r') as uncompressed:
TypeError: coercing to Unicode: need string or buffer, file found
open_cmd=open if compression is None else __import__(compression).open
AttributeError: 'module' object has no attribute 'open'
当我在cmd行上运行python-v时,我得到了以下输出:
>>> 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
[scoleman@ip-192-168-9-132端口(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,试试python 2中的lzmaffi
将open('one-csv-file.xz')压缩为:
lzma.LZMAFile(压缩)为未压缩:
对于未压缩的行:
用(线)填充
摘自Python2.7.x和Python3.3+之间的lzma模块存在一些差异 Python2.7.x没有
lzma.open
和lzma.LZMAFile
不接受类似文件的对象。这里有一个函数,用于以独立于Python版本的方式打开lzma
文件
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)
用法只需传递一个
str
、bytes
或类似文件的对象
with open_lzma_file(myfile,'rt') as f:
pair_info=pd.read_csv(f,engine='c',header=0,index_col=0)
您是否调用了您的文件
lzma.py
?如果是这样,请将其重命名为.Hi@roganjosh,没有名为lzma.py的文件。您是从命令行还是IDE工作的?第一个代码段应适用于python3.3或更高版本。尝试从命令行调用它,将其作为方法列出谢谢@Luca Bezerra。我试图实现yur Solution(请参见对问题的编辑),但我得到了一个不同的错误:lzma.LZMAFile(已压缩,'r')为未压缩:TypeError:强制使用Unicode:需要字符串或缓冲区,找到文件您能看一下吗please@Stacey1.您使用的是什么版本的python?2.您知道导入的lzma
是否正确吗?尝试python-v
,然后importlzma
,并检查路径是否符合您的期望。您的路径中可能安装了另一个lzma包或版本。@LucaBezerra谢谢,我正在运行Python 2.7.12。我可以导入lzma,但不知道如何判断我的版本(lzma.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。这有帮助吗?@Stacey你用什么命令来运行脚本?当您运行Python
时,您粘贴的更新输出似乎使用python2作为默认版本。您是否在某种虚拟环境中运行脚本?如果没有,您可以尝试运行python3.py
?