Skip to content Skip to sidebar Skip to footer

How To Modify The Python Script So That It Prints Upto Only Four Decimals

I have a case.py which reads CASE.dat import math import numpy as np with open('CASE.dat', 'r') as msg: data = msg.readlines() for i, line in enumerate(data[2:]): if len(l

Solution 1:

Format strings are what you are looking for:

https://pyformat.info/

So in order to truncating your entries you could use the following code:

for row in data[2:]:
    if len(row.strip().split()) < 6:
        break
    print("{:1.0f} {:1.4f}".format(row.split()[0],row.split()[4]))

Solution 2:

Lol :). You can do:

import math
import numpy as np

withopen("CASE.dat", "r") as msg:
    data = msg.readlines()

for i, line inenumerate(data[2:]):
   iflen(line.strip().split()) < 6:
      break
   row = list(map(float, line.strip().split()))

   ifround(row[4]) == 1:
       val = 1elifround(row[4]) == 4:
       val = 2

   row[4] = row[4] + val

   ifround(row[4]) == 6:
       row[4] = 6 - row[4]
   elifround(row[4]) == 2:
       row[4] =  np.abs(row[4] - 2)

   
   data[i+2] = " ".join(map(str,row))

for row in data[2:]:
   iflen(row.strip().split()) < 6:
      breakprint (round(float(row.split()[0])),round(float(row.split()[4]),4))

Solution 3:

'Formatting will work for you if printing is your concern'

print("%d %0.4f"%(row.split()[0],row.split()[4]))

This should be comment but writing answer just because of formatting

Post a Comment for "How To Modify The Python Script So That It Prints Upto Only Four Decimals"