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?

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
  • Clean your question – N00b Mar 07 '19 at 11:56
  • what is inside that text file? – N00b Mar 07 '19 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. https://stackoverflow.com/a/20929983/8411228 – Uli Sotschok Mar 07 '19 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. – John Szakmeister Mar 07 '19 at 12:09

2 Answers2

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.

TomMP
  • 715
  • 1
  • 5
  • 15
  • 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 – federico fede Mar 07 '19 at 12:07
  • 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. – Uli Sotschok Mar 07 '19 at 12:09
  • @federico-fede Glad to help! Don't forget to mark as answered if there are no further questions. – TomMP Mar 07 '19 at 12:10
1

Your problem is the operator *

  • 1
    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 – federico fede Mar 07 '19 at 12:09
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/22398140) – vahdet Mar 07 '19 at 12:54
  • Looks like this has solved the problem @vahdet. So it might also be an answer. Can you explain, why the operator * was the problem? – Tobias Theel Mar 07 '19 at 13:12
  • 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. – Leandro Silva Mar 08 '19 at 01:20