Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
906 views
in Technique[技术] by (71.8m points)

numpy.loadtxt, ValueError: could not convert string to float

This is sample from large csv file:

6.1;6.1;7.2;8.9;5.0;
8.9;10.0;8.9;6.1;5.0;

If I try to read it to numpy array with np.loadtxt('test.csv', delimiter=';') I get:

ValueError: could not convert string to float:

and don't understand why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to strip off the trailing ';' from the lines.

A possible workaround if you know you have 5 columns is:

np.loadtxt('test.csv', delimiter=';', usecols=range(5))

Or, use genfromtext instead which handles missing values

np.genfromtxt('test.csv', delimiter=';')[:,:-1]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...