0

I am using Python's format mini-language to reference a dictionary. For one float in a dictionary, I would like to print it without the decimal point.

For example:

a_dict = {'a_float' : 321.5241,
          'b_float' : 0.011322,
          'c_float' : 24.12354}

out = ('1 25142U {a_float:9.3f}\n'
      '2 {b_float}{c_float:10.3f}').format(**a_dict)

print out

Returns:

>>> 1 25142U   321.524
>>> 2 0.011322    24.123

However, I would like to format b_float (to remove '0.') such that printing out returns:

>>> 1 25142U   321.524
>>> 2 011322    24.123

Is this possible within the context of mini-language format specifications?

I'm aware this can be done by (a) passing in the individual items (and editing them in the argument) instead of the dictionary itself or by (b) post-processing the out string, but I was wondering if Python's mini-language is sophisticated enough to simplify this problem solely to the format specifications of the mini-language.

(For those interested in the motivation for this, I'm trying to print out a TLE from a dictionary of satellite parameters. Google/Wikipedia will probably tell you more if you're interested, but the location of each character is important, and it assumes the decimal place for some items like the eccentricity.)

Bryce93
  • 451
  • 7
  • 11
  • 1
    Dumb question: Can you just post-process? If the string otherwise lacks periods, you can just do: `'{a_float}'.format(**a_dict).replace('.', '')` If you want more trailing zeroes, as in your example, you could do that at format time, or follow up with `.ljust(8, '0')` to expand to a fixed width. – ShadowRanger Nov 06 '15 at 01:06
  • The second string isn't what you'd get by removing the decimal point from the first string. Can you give a more precise specification of your desired output? – user2357112 Nov 06 '15 at 01:06
  • `'{:08d}'.format(int(a_dict['a_float']*10**8))` -> `'01132200'` – martineau Nov 06 '15 at 02:03
  • I could post-process, but I'm passing multiple dictionary inputs into the string. I'll update the question to better represent the complexity. – Bryce93 Nov 06 '15 at 18:04

1 Answers1

1

repr convert float to string then split the string

repr(0.011322).split('.')[-1]
or 
repr(0.011322)[2:]

output
011322
galaxyan
  • 5,944
  • 2
  • 19
  • 43