pythonエンジニアリングでtxtファイルに文字(str)と数字(float)が同時に含まれる場合を処理する


1. If you use np.genfromtxt, you could specify  dtype=None , which will tell  genfromtxt  to intelligently guess the dtype of each column. Most conveniently, it relieves you of the burder of specifying the number of bytes required for the string column. (Omitting the number of bytes, by specifying e.g.  np.str , does not work.)
方法1:npを用いる場合.genfromtxtでは、dtype=Noneを指定できます.これはgenfromtxtが各列のdtypeをスマートに推測することを示します.最も便利なのは、文字列を指定するのに必要なバイト数の手間を軽減することです.(例えばnp.str省略バイト数を指定することでは機能しません.)
In [58]: np.genfromtxt('data.txt', delimiter=',', dtype=None, names=('sepal length', 'sepal width', 'petal length', 'petal width', 'label'))
Out[58]: array([(5.1, 3.5, 1.4, 0.2, 'Iris-setosa'),
       (4.9, 3.0, 1.4, 0.2, 'Iris-setosa'),
       (5.8, 2.7, 4.1, 1.0, 'Iris-versicolor'),
       (6.2, 2.2, 4.5, 1.5, 'Iris-versicolor'),
       (6.4, 3.1, 5.5, 1.8, 'Iris-virginica'),
       (6.0, 3.0, 4.8, 1.8, 'Iris-virginica')], 
      dtype=[('sepal_length', '

2.If you do want to use  np.loadtxt , then to fix your code with minimal changes, you could use:
npを使いたいならloadtxtでは、少し変更する必要があります.
np.loadtxt("data.txt", dtype={'names': ('sepal length', 'sepal width', 'petal length', 'petal width', 'label'),
          'formats': (np.float, np.float, np.float, np.float, '|S15')}, delimiter=',', skiprows=0)

The main difference is simply changing  np.str  to  |S15  (a 15-byte string).
Also note that  open("data.txt"), 'r'  should be  open("data.txt", 'r') . But since  np.loadtxt can accept a filename, you don't really need to use  open  at all.参照先:https://stackoverflow.com/questions/23546349/loading-text-file-containing-both-float-and-string-using-numpy-loadtxt