如何将不同类型的数据从文件导入Python Numpy数组? [英] How do I import data with different types from file into a Python Numpy array?
本文介绍了如何将不同类型的数据从文件导入Python Numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个文件myfile.txt
,其中包含:
Say I have a file myfile.txt
containing:
1 2.0000 buckle_my_shoe
3 4.0000 margery_door
如何将文件中的数据作为int,float和string导入到numpy数组中?
我的目标是:
array([[1,2.0000,"buckle_my_shoe"],
[3,4.0000,"margery_door"]])
我一直在玩以下游戏,但无济于事:
I've been playing around with the following to no avail:
a = numpy.loadtxt('myfile.txt',dtype=(numpy.int_,numpy.float_,numpy.string_))
另一种方法可能是使用ndarray类型,然后进行转换.
Another approach might be to use the ndarray type and convert afterwards.
b = numpy.loadtxt('myfile.txt',dtype=numpy.ndarray)
array([['1', '2.0000', 'buckle_my_shoe'],
['3', '4.0000', 'margery_door']], dtype=object)
推荐答案
使用 查看全文