读取压缩数据会得到不同的结果

时间:2016-07-20 07:54:01

标签: python numpy

我正在使用genfromtxt来读取数据。

genfromtxt也必须适用于.gz文件,但它似乎没有。

使用简单数据(不是.gz文件)

f = open('file', 'r')
con = np.genfromtxt(f,dtype=str)

print con
print type(con)

文件内容是:

 @HWI
    ABCDE
    +
    @HWI7
    EFSA
    +
    ???=AF
    GTEY@JF
    GVTAWM

以上代码的输出是:

['@HWI' 'ABCDE' '+' '@HWI7' 'EFSA' '+' '???=AF' 'GTEY@JF' 'GVTAWM']
<type 'numpy.ndarray'>

如果,我只是使用与压缩为.gz文件的aboce文件相同的代码,输出为:

[ "\x1f\x8b\x08\x08\x1b4\x8eW\x00\x03file\x00Sp\xf0\x08\xf7\xe4R\x00\x02G'g\x17W0K\x1bL\x82$\xcc\xc1,W\xb7`G$"
 '{{{[G70\xd3=\xc45\xd2\xc1' '\xca\x0e' 'q' '\xf7\x05' '\x06\x07\xc2P']
<type 'numpy.ndarray'>

问题是我想稍后进行一些计算,我不能这样。

我也试过(对于.gz版本):

with gzip.open(file, 'r') as f:
    con = np.array([f.read()])

print con
print type(con)

给出:

[ ' @HWI\n    ABCDE\n    +\n    @HWI7\n    EFSA\n    +\n    ???=AF\n    GTEY@JF\n    GVTAWM']
<type 'numpy.ndarray'>

更接近最初但仍然无法正常工作(无法继续计算)

如何实现相同的结果?

2 个答案:

答案 0 :(得分:2)

来自文档:

genfromtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)
    Load data from a text file, with missing values handled as specified.

    Each line past the first `skip_header` lines is split at the `delimiter`
    character, and characters following the `comments` character are discarded.

    Parameters
    ----------
    fname : file or str
        File, filename, or generator to read.  If the filename extension is
        `.gz` or `.bz2`, the file is first decompressed. Note that
        generators must return byte strings in Python 3k.

对于gzip版本,您应该尝试直接传递文件名(压缩文件的扩展名为.gz

测试数据:

$> cat ./test.txt
 @HWI
     ABCDE
         +
             @HWI7
                 EFSA
                     +
                         ???=AF
                             GTEY@JF
                                 GVTAWM

$> gzip --stdout ./test.txt > ./test.txt.gz

然后在python:

>>> import numpy as np
>>> np.genfromtxt('./test.txt', dtype=str)
array(['@HWI', 'ABCDE', '+', '@HWI7', 'EFSA', '+', '???=AF', 'GTEY@JF',
       'GVTAWM'],
      dtype='<U7')
>>> np.genfromtxt('./test.txt.gz', dtype=str)
array(['@HWI', 'ABCDE', '+', '@HWI7', 'EFSA', '+', '???=AF', 'GTEY@JF',
       'GVTAWM'],
      dtype='<U7')

答案 1 :(得分:1)

为什么不将gap: https://ssl.gstatic.com https://*.googleapis.com http://example.com http://example2.com ;script-src 'self' 'unsafe-inline' 'unsafe-eval' https://maps.googleapis.com https://maps.gstatic.com https://mts1.googleapis.com https://mts0.googleapis.com http://example.com http://example2.com ;object-src * ; style-src 'self' 'unsafe-inline' https://ssl.gstatic.com https://fonts.googleapis.com http://example.com http://example2.com ; media-src *;img-src *;font-src *"> genfromtxt的文件对象一起使用?

gzip.open()

修改

with gzip.open('file.gz') as f: print(numpy.genfromtxt(f, dtype=str)) numpy.gz文件使用预定义的文件切换器。您可以检查配置,如:

.bz2

在我的机器上,它显示了bz2和gz文件的处理程序:

import numpy.lib._datasource as DS
DS._file_openers._load()
print(DS._file_openers._file_openers)

由于gz文件的处理程序实际上是{'.bz2': <type 'bz2.BZ2File'>, None: <built-in function open>, '.gz': <function open at 0x7efca562a6e0>} ,numpy不会在你的机器上使用它似乎很奇怪。