2
import numpy as np
import datetime
import string


time_1=np.loadtxt('1.csv',delimiter=',',
                  skiprows=0,usecols=(0,),unpack=False)

I use numpy.loadtxt to read a CSV file. as showed above. However, it always has

ValueError: could not convert string to float:

the csv file like this

41795.00

41795.00

41795.02

41795.02

41795.03

2
  • It should work. Maybe your file is of different format than posted here, or has some special characters, etc. Commented Mar 3, 2015 at 5:06
  • I get the csv file by Excel, I don't know if there is some thing different, and when I input type(time_1[o]), it shows "<type 'numpy.string_'>". I am wondering if some method can change the str to float? Commented Mar 3, 2015 at 11:18

1 Answer 1

1

The reason might be you have extra commas at the end of a line. The value error warning usually shows the string that it couldn't convert, but in your case it's a blank, meaning it was trying to convert a blank to a float, which could be the blank after a trailing comma.

The solution might not be within loadtxt which is faulty. Try genfromtxt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.