I have a text file, prob_value.txt
that is generated from a list and holds ints, floats, and scientific notation strings.
ex 4 values. 4 0.004e-2 5.5 0.0
I need to turn them all into floats so I can plug them into an equation. I can turn ints into floats but i'm not sure how to add the scientific notation to float conversion into my existing code.
# grabs the string that fits the regex parameters and writes it to prob_value.txt
# values should be either an int, float, or scientific notation
# then turns them all into floats so I can plug them into an equation
collision_probability1 = re.compile(r"\d+(?:\.\d+(?:e\-+\d+)?)?")
with open("out.txt") as f, open("prob_value.txt", "w") as f_out:
prob = f.read()
probability = re.findall(collision_probability1, prob)
f_out.write(f"{probability[0]}")
for i in range(1, len(probability)):
f_out.write(f" {probability[i]}")
# part I am having trouble with. Any string that matches the regex pattern is
# a scientific notation string. As it goes through the for loop, I want the code
# to convert those strings to a float so when I print(probability)
# all the outputs are floats.
sci_not = re.compile(r"\d+\.\d+e\-+\d+")
if sci_not:
float(probability[i])
probability = list(map(float, probability))
print (probability)