Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy中genfromtxt的comments参数_Python_Python 3.x_Numpy_Genfromtxt - Fatal编程技术网

Python numpy中genfromtxt的comments参数

Python numpy中genfromtxt的comments参数,python,python-3.x,numpy,genfromtxt,Python,Python 3.x,Numpy,Genfromtxt,我正在学习numpy中genfromtxt的I/O功能。 我尝试了numpy用户指南中的一个示例。它是关于genfromtxt的comments参数的 以下是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 th

我正在学习numpy中genfromtxt的I/O功能。 我尝试了numpy用户指南中的一个示例。它是关于genfromtxt的comments参数的

以下是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)
我在下面尝试:

>>> 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)
结果出来:

>>> 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:空输入文件:“” warnings.warn('genfromtxt:空输入文件:“%s”“%fname”)

我知道问题在于数据。谁能教我如何设置示例中所示的数据?
非常感谢。

请尝试下面的内容。首先,不要使用
“\”
。其次,为什么要使用
.BytesIO()
使用
StringIO()


试试下面。首先,不要使用
“\”
。其次,为什么要使用
.BytesIO()
使用
StringIO()


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
适用于任何提供行的iterable。所以我给了它一个行列表——用拆分行生成。Py3中的
StringIO
(或
ByteIO
)也可以工作,但需要额外的工作

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

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']

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
适用于任何提供行的iterable。所以我给了它一个行列表——用拆分行生成。Py3中的
StringIO
(或
ByteIO
)也可以工作,但需要额外的工作

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

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']

对于第一个genfromtxt参数,请尝试使用StringIO而不是BytesIO。为什么要在数据中插入反斜杠?对于第一个genfromtxt参数,请尝试使用StringIO而不是BytesIO。为什么要在数据中插入反斜杠?@hpaulj StringIO在py3中的行为与py2不同,而不是py3的用户。感谢您提供的多行字符串输入方法。我有三重引号的问题,“\”
genfromtxt
希望它的输入是字节字符串(没有unicode)。Py3使用unicode字符串作为默认格式,
StringIO
处理默认格式(无论版本如何)。这是2和3之间第二个最常见的区别(最常见的是
打印(…)
表达式)@hpaulj StringIO在py3中的行为与py2不同,而不是py3的用户。感谢您提供的多行字符串输入方法。我有三重引号的问题,“\”
genfromtxt
希望它的输入是字节字符串(没有unicode)。Py3使用unicode字符串作为默认格式,
StringIO
处理默认格式(无论版本如何)。这是2和3之间第二常见的区别(最常见的是
打印(…)
表达式)。谢谢。删除所有的“\”之后,我就可以得到结果了。我正在使用python3.5,所以我需要使用.BytesIO()。我不使用py3,所以我没有这个问题。。祝你好运,谢谢。删除所有的“\”之后,我就可以得到结果了。我正在使用python3.5,所以我需要使用.BytesIO()。我不使用py3,所以我没有这个问题。。祝你好运