0

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(line.strip().split()) < 6:
      break
   row = list(map(float, line.strip().split()))

   if round(row[4]) == 1:
       val = 1
   elif round(row[4]) == 4:
       val = 2

   row[4] = row[4] + val

   if round(row[4]) == 6:
       row[4] = 6 - row[4]
   elif round(row[4]) == 2:
       row[4] =  np.abs(row[4] - 2)

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

for row in data[2:]:
   if len(row.strip().split()) < 6:
      break
   print (row.split()[0],row.split()[4])

It give me data like

1.0 0.06409900000000013
2.0 -0.033354000000000106
3.0 -0.008829999999999671

and so on I am looking for a modification in such a way that it prints the data like

1  0.0640
2 -0.0333
3 -0.0088
quamrana
  • 37,849
  • 12
  • 53
  • 71
astha
  • 593
  • 5
  • 15
  • use `format` function https://www.kite.com/python/answers/how-to-print-a-float-with-two-decimal-places-in-python#:~:text=Use%20str.,float%20with%20two%20decimal%20places&text=format(number)%20with%20%22%7B,number%20with%20two%20decimal%20places. – gsb22 Jan 11 '21 at 09:06
  • or use [f-strings](https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings). – quamrana Jan 11 '21 at 09:07
  • Check this - https://stackoverflow.com/questions/45310254/fixed-digits-after-decimal-with-f-strings – Vaebhav Jan 11 '21 at 09:10

3 Answers3

2

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]))
MalteHerrmann
  • 198
  • 1
  • 14
1

Lol :). You can do:

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(line.strip().split()) < 6:
      break
   row = list(map(float, line.strip().split()))

   if round(row[4]) == 1:
       val = 1
   elif round(row[4]) == 4:
       val = 2

   row[4] = row[4] + val

   if round(row[4]) == 6:
       row[4] = 6 - row[4]
   elif round(row[4]) == 2:
       row[4] =  np.abs(row[4] - 2)

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

for row in data[2:]:
   if len(row.strip().split()) < 6:
      break
   print (round(float(row.split()[0])),round(float(row.split()[4]),4))
Synthase
  • 5,849
  • 2
  • 12
  • 34
  • Traceback (most recent call last): File "case.py", line 30, in print (round(row.split()[0]),round(row.split()[4],4)) TypeError: type str doesn't define __round__ method – astha Jan 11 '21 at 09:10
  • already corrected :) – Synthase Jan 11 '21 at 09:10
  • Worked like a charm. thank you. I can accept the answer in 8 minutes. So I need to wait for few more minutes. – astha Jan 11 '21 at 09:12
0

'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

DexJ
  • 1,264
  • 13
  • 24