我有一个文本文件,它包含字符串、整数和浮点形式的信息行,用空格隔开,例如
HIP893 23 U 10 7 0.028
4
HIP1074 43_20 20 0.0141 1
HIP11325 23_10 7 0.02388 5
... 在
我已使用以下行导入此数据:
data=np.genfromtxt('98_info.txt', dtype=(object, object, int,float,float))
但是,当我这样做时,我得到的输出
^{pr2}$我希望没有“b”,而是:
[('HIP893', '23_10', 7, 0.028, 4.0)
('HIP1074', '43_20', 20, 0.0141, 1.0)
('HIP1325', '23_10', 7, 0.02388, 5.0)
... ]
我试过纽比的机芯、机芯但是这给了我一个错误“字符串操作在非字符串数组上”,我想是因为我的数据是字符串和数字的组合?在
是否有某种方法可以删除字符但将数据保留在数组中,或者也许有另一种方法来加载信息,使字符串保留在引号中,而没有引号的数字呢?在
如果有一种方法可以将它作为2d np数组导入,那就更好了,但如果不是的话,那就不是问题了。在
谢谢!在
您可以使用解码字节字符串的函数传递
converters=
,例如:这将给您
^{pr2}$data
的:您的样品和
dtype
:使用
^{pr2}$dtype=None
(和encoding=None
):指定unicode数据类型(必须包括大小):
我很困惑为什么
None
为第二列选择一个整型数据类型(下划线应该可以阻止这一点)。在不带encoding参数的
dtype=None
将引发此警告:在Py2中,默认的字符串类型是bytestrings;在py3unicode中。
genfromtxt
与py2兼容使用了bytestrings
。但是最近的版本增加了encoding
参数。但这种转变似乎仍有一些粗糙的边缘。在这可能就是我得到
i8
;Python自己的int
接受下划线的原因。在后跟
b
的字符串是编码字符串,即在bytes
中您可以通过应用
decode
函数或仅使用str
来解码它们我想你可以通过this在nparray中转换它,所以回答
相关问题 更多 >
编程相关推荐