Is it enough to print a list of float lists?

Basically, I have to dump a series of temperature readings into a text file. This is a list of elements separated by spaces, where each line represents something (I don’t know, and it just becomes mandatory in the fortran model, shuddering). I more or less process it from the side of our groups, which extracts these temperature readings and dumps them into a text file.

Basically a quick example: I have a list similar to this (but with a lot of elements):

temperature_readings = [ [1.343, 348.222, 484844.3333], [12349.000002, -2.43333]]

In the past, we simply dumped this into a file, unfortunately, there are people who have this annoying dexterity who wants to look directly at the text file, and select certain columns and change some things (for testing .. I don’t really know ..) . But they always complain that the columns aren't lining up correctly, they are pretty much listed above:

    1.343     348.222     484844.333
12349.000002   -2.433333

So, these beautiful decimal places line up. Is there an easy way to do this?

+3
source share
5 answers

you can edit like this:

str = '%-10f' % val

on the left panel:

set = '%10f' % val

or in combination and set the accuracy to 4 decimal places:

str = '%-10.4f' % val

:

import sys
rows = [[1.343, 348.222, 484844.3333], [12349.000002, -2.43333]]
for row in rows:
  for val in row:
    sys.stdout.write('%20f' % val)
  sys.stdout.write("\n")

        1.343000          348.222000       484844.333300
    12349.000002           -2.433330
+3
source

The% (String formatting) operator is now deprecated.

You can use str.format to print in Python.

- :

for set in temperature_readings:
    for temp in set:
        print "{0:10.4f}\t".format(temp),
    print

:

    1.3430        348.2220      484844.3333
12349.0000         -2.4333

: http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting

+2

(, , , ), - :

for line in temperature_readings:
   for value in line:
      print '%10.2f' % value,
   print

:

      1.34     348.22  484844.33
  12349.00      -2.43
+1

Python 2. *,

for sublist in temperature_readings:
    for item in sublist:
        print '%15.6f' % item,
    print

       1.343000      348.222000   484844.333300
   12349.000002       -2.433330

. , , !

+1

Python pprint.

pprint " " Python , .

[...]

pprint() .

>>> import pprint
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> stuff = ['a' * 10, tup, ['a' * 30, 'b' * 30], ['c' * 20, 'd' * 20]]
>>> pprint.pprint(stuff)
['aaaaaaaaaa',
 ('spam',
  ('eggs',
   ('lumberjack',
    ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, depth=3)
['aaaaaaaaaa',
 ('spam', ('eggs', (...))),
 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]
>>> pprint.pprint(stuff, width=60)
['aaaaaaaaaa',
 ('spam',
  ('eggs',
   ('lumberjack',
    ('knights',
     ('ni', ('dead', ('parrot', ('fresh fruit',)))))))),
 ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'],
 ['cccccccccccccccccccc', 'dddddddddddddddddddd']]

Otherwise, find out what is the longest number in terms of character length, and use Python's string replacement formatting options to make everything the same length, Or use tabs as separator characters. Lots of options!

-1
source

Source: https://habr.com/ru/post/1729719/


All Articles