繁体   English   中英

numpy中genfromtxt的注释参数

[英]The comments argument of genfromtxt in numpy

我正在numpy中学习genfromtxt的I / O功能。 我尝试了numpy用户指南中的示例。 它与genfromtxt的comment参数有关。

这是numpy用户指南中的示例:

>>> data = """#
... # Skip me !
... # Skip me too !
... 1, 2
... 3, 4
... 5, 6 #This is the third line of the data
... 7, 8
... # And here comes the last line
... 9, 0
... """
>>> np.genfromtxt(StringIO(data), comments="#", delimiter=",")
[[ 1. 2.]
[ 3. 4.]
[ 5. 6.]
[ 7. 8.]
[ 9. 0.]]

我在下面尝试过:

data = """#                 \
    # Skip me !         \
    # Skip me too !     \
    1, 2                \
    3, 4                \
    5, 6 #This is the third line of the data    \
    7, 8                \
    # And here comes the last line  \
    9, 0                \
    """
a = np.genfromtxt(io.BytesIO(data.encode()), comments = "#", delimiter = ",")
print (a)

结果出来了:

genfromtxt:空输入文件:“ <_io.BytesIO对象位于0x0000020555DC5EB8>” warnings.warn('genfromtxt:空输入文件:“%s”'%fname)

我知道问题出在数据上。 任何人都可以教我如何如示例中所示设置数据? 非常感谢。

请尝试以下。 首先,不要使用"\\" 其次,为什么要使用.BytesIO() StringIO()

import numpy as np
from StringIO import StringIO

data = """#                 
    # Skip me !     
    # Skip me too !     
    1, 2                
    3, 4                
    5, 6 #This is the third line of the data    
    7, 8                
    # And here comes the last line  
    9, 0                
    """

    np.genfromtxt(StringIO(data), comments="#", delimiter=",")

    array([[ 1.,  2.],
           [ 3.,  4.],
           [ 5.,  6.],
           [ 7.,  8.],
           [ 9.,  0.]])

ipython3 (py3)交互式会话中,我可以执行以下操作:

In [326]: data = b"""#
     ...: ... # Skip me !
     ...: ... # Skip me too !
     ...: ... 1, 2
     ...: ... 3, 4
     ...: ... 5, 6 #This is the third line of the data
     ...: ... 7, 8
     ...: ... # And here comes the last line
     ...: ... 9, 0
     ...: ... """
In [327]: 
In [327]: data
Out[327]: b'#\n# Skip me !\n# Skip me too !\n1, 2\n3, 4\n5, 6 #This is the third line of the data\n7, 8\n# And here comes the last line\n9, 0\n'
In [328]: np.genfromtxt(data.splitlines(),comments='#', delimiter=',')
Out[328]: 
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.],
       [ 7.,  8.],
       [ 9.,  0.]])

在Python3中,字符串必须为字节; 在Py2中,这是默认设置。

对于多行字符串输入(三引号),请勿使用\\ 这是行的延续。 您想保留\\n

data = b"""
one
two
"""

注意,我还可以使用:

data = '#\n# Skip me\n...'

带有显式\\n

genfromtxt与任何可迭代的可迭代行一起使用。 所以我给了它一个行列表-用分割线产生的。 StringIO (或Py3中的ByteIO )也可以工作,但它可以额外工作。

当然,另一个选择是将这些行复制到文本编辑器中,并将它们另存为简单的文本文件。 将n复制粘贴到交互式会话中是一个方便的快捷方式,但不是必需的。

In [329]: data.splitlines()
Out[329]: 
[b'#',
 b'# Skip me !',
 b'# Skip me too !',
 b'1, 2',
 b'3, 4',
 b'5, 6 #This is the third line of the data',
 b'7, 8',
 b'# And here comes the last line',
 b'9, 0']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM