'ValueError while importing data from a file using Python
I keep getting ValueError: could not convert string to float:' '.
The code I used is:
import matplotlib.pyplot as plt
import numpy as np
X, Y = np.loadtxt('/Users/sul/Desktop/2,54,51PM.txt', delimiter=',', unpack=True)
plt.plot(X, Y)
plt.title('Line Graph using NUMPY')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
The file contains two columns and over 1000 rows, like this:
1413.541000000 0.001121856
1413.548812500 0.001122533
1413.556625000 0.001121994
1413.564437500 0.001120641
1413.572250000 0.001120932
What can I do to fix this?
Solution 1:[1]
Take a look at this line from your code:
X, Y = np.loadtxt('/Users/sul/Desktop/2,54,51PM.txt', delimiter=',', unpack=True)
If your data file looks like the sample you posted, then there is no comma being used as a delimiter.
It seems like it's looking at these two float values separated by a space and interpreting that as a String.
You need to match the delimiter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jorge Gx |
