-1

so I have txt file which has the following 2 values 0.8812395E-5.1.000E+01 but when printing on python it says that the list's two components are [0.8812395E-5.1.000E+01, ''] basically instead of considering the two values separately it takes the two as one, I need to extract the two numbers.

pressure = open('pressure.txt')
pressure = pressure.read()
line = pressure.split('\n')
print(len(line))
print(line)

and the result

2
['.8812395E-15.1000000E+01', '']
Process finished with exit code 0
  • 2
    " instead of considering the two values separately" - the problem is that your values are separated by nothing. If you can, you should try to obtain better structured data first. – Thierry Lathuille Jan 11 '21 at 13:29
  • Try splitting it by `'-'` as they are not in two lines but in one separated by a dash, if I understand your notation correctly. – Jonas Palačionis Jan 11 '21 at 13:29
  • Is it possible to modify the format of the text file? It would be easier to split them using a unique character to separate them, such as a comma. Right now they are separated with a decimal which is used also in the scientific notation. – big_bad_bison Jan 11 '21 at 13:31
  • @big_bad_bison The values are not separated by a dot, it's part of the second value: `.1000E+01' – Thierry Lathuille Jan 11 '21 at 13:33
  • 4
    Your current data is ambiguous. It could be interpreted as `.8812395E-15` and `.1000000E+01`, or as `.8812395E-1` and `5.1000000E+01`. As others have said, you need to separate the values in some way. – l4mpi Jan 11 '21 at 13:34
  • 1
    Where does the second value begins? – LoukasPap Jan 11 '21 at 13:34
  • the values are separated by the dot '.' so they are .8812395E-15 and .1000000E+01 – pickashoe Jan 11 '21 at 13:39
  • @ThierryLathuille If the first value has a leading zero, I would assume the second would also have a leading zero then? In which case it has to be a period separating them I think? Regardless, the formatting needs to be changed. – big_bad_bison Jan 11 '21 at 13:39

2 Answers2

-1

Are the numbers on the same line? You could use

line.replace('.','#0.').split('#',2)

You then just need to drop the first blank value.

-1

As stated in the comments, if you can you should change the delimiter between values if you can. Since the decimal is used in the scientific notation, it is difficult to effectively split the string. If you can use a comma instead, it becomes much easier.

If you can't modify the formatting, this code will split the string on all decimals, then piece the values back together.

pressure = '0.8812395E-5.1.000E+01'
seg = pressure.split('.')
for i in range(0, len(seg), 2):
    f = float('.'.join(seg[i:i + 2]))
    print(f)

Output:

8.812395e-06
10.0
big_bad_bison
  • 1,021
  • 1
  • 7
  • 12