java_doctor_101
java_doctor_101

Reputation: 3357

Conversion error in numpy.loadtxt()

I am trying to plot two columns from a text file using python matplotlib but I am getting

ValueError: invalid literal for float(): 148.000000;

This is my python script

import numpy as np
import matplotlib.pyplot as plt
x,y = np.loadtxt('sharma5.txt')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.show()

and here is a part of my text file

36.000000   61.000000
36.000000   61.000000
36.000000   148.000000;
36.000000   60.000000
36.000000   120.000000
36.000000   77.000000
36.000000   160.000000

Thanks in advance..

Upvotes: 1

Views: 4041

Answers (2)

deprecated
deprecated

Reputation: 2185

In case you don't want to fix your data file, you can use the converters option to loadtxt in order to remove any extraneous semicolons. Something like np.loadtxt("sharma5.txt", converters = {1: lambda s: float(s.strip(";"))}) should work.

Upvotes: 1

ptomato
ptomato

Reputation: 57854

The problem is the semicolon in your text file, which is not recognized as a legal character for converting to a number. Fix the bug in the program that generated that text file.

Upvotes: 0

Related Questions