20 lines
765 B
Python
20 lines
765 B
Python
def reduce_precision(input_file, output_file):
|
|
with open(input_file, "r") as infile, open(output_file, "w") as outfile:
|
|
for line in infile:
|
|
line = line.strip()
|
|
if line: # skip empty lines
|
|
try:
|
|
num = float(line)
|
|
if num.is_integer():
|
|
outfile.write(f"{int(num)}\n")
|
|
else:
|
|
outfile.write(f"{num:.3f}\n")
|
|
except ValueError:
|
|
# If not a number, write unchanged
|
|
outfile.write(line + "\n")
|
|
|
|
if __name__ == "__main__":
|
|
input_path = "data.txt" # original file
|
|
output_path = "data2.txt" # new file
|
|
reduce_precision(input_path, output_path)
|
|
|