Reputation: 71
I have an output that is written in an excel file. My values are float and in python the float point is in dot (1/3 = 0.33), but in excel float points are in commas (0,33). How can I convert all the dots to commas........................................?
import math
print("\nThis program calculates the maximum height and speed of a one stage rocket \n")
Isp = float(input("Write Specific Impulse in seconds = "))
min = float(input("Write the initial mass of the rocket ="))
mfuel = float(input("Write tha mass of fuel = "))
mf = float(input("Write the final mass of the rocket = "))
tb = float(input("Write the time that rockets fuel ends = "))
file = open("Ask_2_AET.csv", "w")
file.write("h in meters")
file.write(";")
file.write("V in m/s")
file.write(";")
file.write("t in seconds\n")
g = 9.81
t = 0.000001
Ve = g * Isp
while t == tb or t < tb:
mt = min - mfuel * (t/tb)
y = g * (-t * Isp * (math.log(min/mt) / ((min/mt) - 1)) + t * Isp - 1/2 * t ** 2)
V = Ve * math.log(min / mt) - g * t
t = round(t)
file.write(str(round(y, 2)))
file.write(";")
file.write(str(round(V, 2)))
file.write(";")
file.write(str(round(t)))
file.write("\n")
t += 1
Vb = V
while V > 0:
V = Vb - g * t
h = Vb * t - 1 / 2 * g * t ** 2
if V > 0:
file.write(str(round(h, 2)))
file.write(";")
file.write(str(round(V, 2)))
file.write(";")
file.write(str(round(t)))
file.write("\n")
t += 1
else:
break
Upvotes: 0
Views: 280
Reputation: 425
Just use
variable.replace(".",",")
If they are not strings you might need to do str() first.
Upvotes: 0
Reputation: 137
is a csv you're reading ? if yes, you should consider the csv module which will allows you to parse your file easily. Then, you can convert all your str to float by using .replace() like this:
my.csv: 1,90;1,90;1,90 2,91;2,92;2,93 3,92;3,92;3,93
>>> import csv
>>> with open('my.csv') as f:
... for line in f.readlines():
... line.replace(',' , '.')
...
'1.90;1.90;1.90\n'
'2.91;2.92;2.93\n'
'3.92;3.92;3.93\n'
Upvotes: 0
Reputation: 380
you can use the replace()
method for strings
in python. Just before writing you can convert the number to string and use the replace()
method to replace the dot into comma:
num = 3.16
num = str(num).replace(".", ",")
Upvotes: 1