0

I have a problem with a command of python. I want my program to read a specific line from my file, and here I haven't problem. The problem is when I have to convert the line in a float (I need of float to calculate some equation). My program is:

f=open('coeff.txt')
lines=f.readlines()

k1=lines[0]

k1 = float(k1)


k2=lines[1]

k2 = float(k2)


k3=lines[2]

k3 = float(k3)

k4=lines[3]

k4 = float(k4)

And the file coeff.txt is:

1.2*1e-1   

6.00*1e-34

1.13*1e-4

6.9*1e-16

that is 1.2*10^(-1) , 6*10^(-34), 1.13*10^(-4), 6.9*10^(-16)

and I get the error:

ValueError: could not convert string to float: '6.00*1e-34\n'

(obviously that this error is referred to each line.

Can you help me, please?

4
  • Clean your question Commented Mar 7, 2019 at 11:56
  • what is inside that text file? Commented Mar 7, 2019 at 12:00
  • here is a useful answer, that shows what are floats and what not. Maybe you just need to format your string before you convert it to a float. stackoverflow.com/a/20929983/8411228 Commented Mar 7, 2019 at 12:05
  • If you're in control of what is in coeff.txt, it'd probably be better to drop the "*1" before the "e". That's a non-standard notation. Much better to just say 1.2e-1, 6e-34, 1.13e-4, and 6.9e-16, IMHO. Commented Mar 7, 2019 at 12:09

2 Answers 2

2

Python doesn't know how to interpret '6.00*1e-34\n' as a float. You will have to clean your data before you can actually use it.

Eventually, you will want to have each line in a format like this: 6.00e-34

Looking at it closely, it seems like the only differences are the \n at the end of the line, and the 1* in the middle.

You can get rid of the newline character at the end of the string (\n) by calling the .strip() method, and replace *1 with an empty string to get to the above format.

val = '6.00*1e-34\n'
cleaned_val = val.strip().replace('*1', '')
print(float(cleaned_val))
>>> 6e-34

Edit: It seems like the presence of the newline character doesn't really matter - so you only need to replace the *1 part of your string. I'm leaving it in anyway.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much!! i have resolved my problem!! in fact now i have wrote my file "coeff.txt" as : 0.12 6.00e-34 1.13e-4 6.9e-16 and now it work
I just tested converting a string to float with a \n inside and it just works. So your hint is not necessary. But it will of course also works, when you strip it.
@federico-fede Glad to help! Don't forget to mark as answered if there are no further questions.
1

Your problem is the operator *

4 Comments

thank you so much!! i have resolved my problem!! in fact now i have wrote my file "coeff.txt" as : 0.12 6.00e-34 1.13e-4 6.9e-16 and now it work
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
Looks like this has solved the problem @vahdet. So it might also be an answer. Can you explain, why the operator * was the problem?
open() return a string(default) or bytes(b) objects, when float() receives a string it should contain a decimal number positive or negative and accept ONLY '+' or '-' sign operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.