Search code examples
python-3.xnumpygenfromtxt

How do I specify dtype str correctly?


File is utility.dat

Name    D   L   J   H   E   M   RF  AF
line1   150 4.5 2.0 150 2   Copper  0.8 true
line2   140 4.5 2.0 140 2   Aluminium   0.8 true

My script is script.py

import numpy
configName = "utility.dat"
lines = numpy.genfromtxt(configName,
                         skip_header=1,
                         dtype=(str, float, float, float, float, float, str, float, bool))

print(lines[0])

My result is

('',  150.,  4.5,  2.,  150.,  2., '',  0.8,  True)

Process finished with exit code 0

Now how do I specify the dtype correctly? Because I don't want to give a size like "S12" or something because there is no way I will know how big this can get. I also don't want to use dtype=None.

Any other solution?


Solution

  • Since i am not able to do this via NumPy i have moved over to Pandas and it worked for me.

    I am new to Python and so still learning, but i get the idea that if i want to have a table of data with Float,String,Integers i am better off using Pandas and if i have pure table that is mostly Float and Integers i am better off using NumPy.

    for now Pandas worked for me, and so moving on. Thank you