3

I've imported a csv file which all contain decimals with exponents for example (5.5006250364943992**02). I keep getting ValueError: could not convert string to float. This is what I have done:

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()
1
  • show us what's in your csv file? A sample atleast? Commented Jun 27, 2017 at 11:59

1 Answer 1

4

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

If by "**" mean "10^" then you would need to manually make that replacement.

tmp = row[0].replace("**","e")
x.append(tmp)
Sign up to request clarification or add additional context in comments.

4 Comments

Nice noticing that..seems like that wouldve caused the problem+1!
I tried what you said and it didn't work. so instead I changed all my values to values in this form '5.5006250364943992*10**(02)' and 5.5006250364943992*10^(02) instead of what i wrote in my question but still says that it can't convert string to float.
The problem is that Python does not intepret the string (5.5*10**3 is a valid expression in python, but not a valid formatting for a number string). Can you write the output or explain why my solution did not work? I tried it on my computer and it worked, but you would need to make the same replacement for the y values as well. The formatting you want is "E-notation" and if you don't want to change the CVS files you can easily change it in the code.
It worked! thank you. You were right, it was the e notation that was the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.