tags:

views:

92

answers:

5

Basically i have to dump a series of temperature readings, into a text file. This is a space delimited list of elements, where each row represents something (i don't know, and it just gets forced into a fortran model, shudder). I am more or less handling it from our groups side, which is extracting those temperature readings and dumping them into a text file.

Basically a quick example is i have a list like this(but with alot more elements):

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

In the past we just dumped this into a file, unfortunately there is some people who have this irritating knack of wanting to look directly at the text file, and picking out certain columns and changing some things (for testing.. i don't really know..). But they always complain about the columns not lining up properly, they pretty much the above list to be printed like this:

    1.343     348.222     484844.333
12349.000002   -2.433333

So those wonderful decimals line up. Is there an easy way to do this?

+3  A: 

you can right-pad like this:

str = '%-10f' % val

to left pad:

set = '%10f' % val

or in combination pad and set the precision 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
jspcal
The only problem with this is that the % operator is deprecated.
Swiss
@swiss, % (modulo operator) is the most portable. the newer str.format isn't available in most installations
jspcal
@jspcal This is true, if there is no possibility of using str.format, then % is the right choice. str.format should be preferred when possible though.
Swiss
+1  A: 

If you also want to display a fixed number of decimals (which probably makes sense if the numbers are really temperature readings), something like this gives quite nice output:

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

Output:

      1.34     348.22  484844.33
  12349.00      -2.43
sth
A: 

Check this out:

Python pprint.

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

Chris Cameron
For inspecting arrays during development, also see `numpy` - it has builtin excellent pretty-printing of numerical arrays. But both `numpy` and `pprint` add brackets around the numbers, so they won't work if you need a clean numbers-only output.
Beni Cherniavsky-Paskin
+1  A: 

In Python 2.*,

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

emits

       1.343000      348.222000   484844.333300
   12349.000002       -2.433330

for your example. Tweak the lengths and number of decimals as you prefer, of course!

Alex Martelli
you want a `print` at the end of the outer loop, of course.
Beni Cherniavsky-Paskin
@Beni, right, I copy-and-pasted one line fewer than I shd have, let me edit to fix, thanks.
Alex Martelli
+1  A: 

The % (String formatting) operator is deprecated now.

You can use str.format to do pretty printing in Python.

Something like this might work for you:

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

Which prints out the following:

    1.3430        348.2220      484844.3333
12349.0000         -2.4333

You can read more about this here: http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting

Swiss
Note that `%s is deperecated only the "`.format()` is better" sense; `%` formatting is NOT going to disappear.
Beni Cherniavsky-Paskin
sys.stdout.write? What's wrong with print or print() if you want to stick to Python 3? Your code looks like Java written with Python syntax.
Max Shawabkeh
@ Max S. Force of habbit. There's no real reason not to use print instead.
Swiss