ValueError: failed to convert string to float: plotting in python

I imported a csv file that contains decimal numbers with exponents, for example (5.5006250364943992 ** 02). I keep getting ValueError: could not convert string to float

. This is what I did:

import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('DNSdata.csv', 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append(float(row[0]))
        y.append(float(row[1]))
plt.plot(x, y, label='DNSdata')
plt.xlabel('x')
plt.ylabel('y')
plt.title('DNSdata')
plt.show()

      

+3


source to share


1 answer


The syntax that you wrote The syntax that is used in the file? I don't think Python can intepret "5.5 ** 02".

If "**" means "10 ^", you will need to manually make this replacement.



tmp = row[0].replace("**","e")
x.append(tmp)

      

+4


source







All Articles