Reading data from text file with missing values

前端 未结 3 1188
萌比男神i
萌比男神i 2020-12-16 16:03

I want to read data from a file that has many missing values, as in this example:

1,2,3,4,5
6,,,7,8
,,9,10,11

I am using the numpy.loadtxt

3条回答
  •  庸人自扰
    2020-12-16 17:01

    This is because the function expects to return a numpy array with all cells of the same type.

    If you want a table with mixed strings and number, you should read it into a structured array instead, also you probably want to add skip_header=1 to skip the first line, ie in your case something like:

    np.genfromtxt('upeak_names.txt', delimiter="\t", dtype="S10,S10,f4,S10,f4,S10,f4", 
    names=["id", "name", "Distance", "name2", "Distance2", "name3", "Distance3], skip_header=1)
    

    See also:

    • Documentation for genfromtxt: https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.genfromtxt.html

    • Documentation for structured arrays in numpy:
      https://docs.scipy.org/doc/numpy-1.15.0/user/basics.rec.html

提交回复
热议问题