How do I import data with different types from file into a Python Numpy array?

前端 未结 2 1614
Happy的楠姐
Happy的楠姐 2020-12-06 17:31

Say I have a file myfile.txt containing:

1   2.0000  buckle_my_shoe
3   4.0000  margery_door

How do I import data from

相关标签:
2条回答
  • 2020-12-06 17:56

    Use numpy.genfromtxt:

    import numpy as np
    np.genfromtxt('filename', dtype= None)
    # array([(1, 2.0, 'buckle_my_shoe'), (3, 4.0, 'margery_door')], 
    #       dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '|S14')])
    
    0 讨论(0)
  • 2020-12-06 18:06

    Pandas can do that for you. The docs for the function you could use are here.

    Assuming your columns are tab separated, this should do the trick (adapted from this question):

    df = DataFrame.from_csv('myfile.txt', sep='\t')
    array = df.values # the array you are interested in
    
    0 讨论(0)
提交回复
热议问题