Zebraboard
Zebraboard

Reputation: 210

np.loadtxt: could not convert string to float

I have the following textfile:

13,1980,0,1,0,0,14,0,1.1975401639938354,1
13,1981,0,2,0,0,14,1,1.853060007095337,4
13,1982,0,3,0,0,14,0,1.3444616794586182,9
13,1983,0,4,0,0,14,0,1.4332133531570435,16
13,1984,0,5,0,0,14,0,1.5681251287460327,25
13,1985,0,6,0,0,14,0,1.6998909711837769,36
13,1986,0,7,0,0,14,0,-0.7202625870704651,49

When I try to load it into Jupyter Notebook in python:

data = np.loadtxt("/filepath/wagepan.txt")

I get the following error:

ValueError: could not convert string to float: '13,1980,0,1,0,0,14,0,1.1975401639938354,1'

Does anyone have any idea how to fix this? I tried looking at other posts, but the issue was not similar.

Upvotes: 2

Views: 457

Answers (1)

Synthaze
Synthaze

Reputation: 6090

Use the delimiter argument to define the delimiter.

import numpy as np

data = np.loadtxt("data.dat", delimiter=",")

print(data)

Output:

[[ 1.30000000e+01  1.98000000e+03  0.00000000e+00  1.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
   1.19754016e+00  1.00000000e+00]
 [ 1.30000000e+01  1.98100000e+03  0.00000000e+00  2.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  1.00000000e+00
   1.85306001e+00  4.00000000e+00]
 [ 1.30000000e+01  1.98200000e+03  0.00000000e+00  3.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
   1.34446168e+00  9.00000000e+00]
 [ 1.30000000e+01  1.98300000e+03  0.00000000e+00  4.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
   1.43321335e+00  1.60000000e+01]
 [ 1.30000000e+01  1.98400000e+03  0.00000000e+00  5.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
   1.56812513e+00  2.50000000e+01]
 [ 1.30000000e+01  1.98500000e+03  0.00000000e+00  6.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
   1.69989097e+00  3.60000000e+01]
 [ 1.30000000e+01  1.98600000e+03  0.00000000e+00  7.00000000e+00
   0.00000000e+00  0.00000000e+00  1.40000000e+01  0.00000000e+00
  -7.20262587e-01  4.90000000e+01]]

Upvotes: 2

Related Questions