0

I'm trying to read values from a CSV file, but I'm getting an error.

Here is code snippet:

import numpy as np

file_read=np.loadtxt('stock_px.csv', delimiter=',')
print ("Restored data: \n" + str(file_read))

Here is error message:

  ValueError: could not convert string to float: 
charlesreid1
  • 4,360
  • 4
  • 30
  • 52
Michael
  • 23
  • 7

1 Answers1

1

Look up the documentation for np.loadtxt and type help(np.loadtxt) into your interpreter.

It has an optional dtype argument which is the data type that it tries to turn the data into. By default this is float. There are strings in your csv which np.loadtxt is unable to interpret as floats. If it's all strings you can set dtype=str instead.

Denziloe
  • 7,473
  • 3
  • 24
  • 34