Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3.2 Str.format value repetition

I'm generating specifically formated input files for a program, and using a small python tkinter GUI frontend to do so. The old code made use of fortran format statements. Unless there's already a direct conversion set of functions for python (which I haven't found), I figured python's formatting would do the job. In general it can, but I can't find a way to have repetition of a certain value:

For example, in fortran:

FORMAT (2A1, I3, **12I5**, F8.3, A7). The "12I5" statement translates to 12 integer values of width 5.

I know I could textually have 12 items in my format call (e.g: ...{0:5d}, {1:5d}, {2:5d}....), but I was wondering if there's a way to have a simplified form like the above fortran example.

Is there something I missed, or is this not possible and I must explicitly write out every item in the format?

-Cheers, Chris.

edit Here is a more clear example of what I'm currently doing:

>>> ---tester = ["M", "T", 1111, 2222, 234.23456, "testing"]    
>>> ---fmt = "{0:1}{1:1}, {2:3d}, {3:5d}, {4:8.3F}, {5:>7}"    
>>> ---print(fmt.format(*tester))    
MT,  13,  1234,  234.235, testing

I'd like to be able to

>>> ---tester = ["M", "T", 1111, **2222, 3333, 4444**, 234.23456, "testing"]    
>>> ---fmt = "{0:1}{1:1}, {2:3d}, **3*{3:5d}**, {4:8.3F}, {5:>7}"    
>>> ---print(fmt.format(*tester))       
like image 500
Ponml Avatar asked May 04 '11 22:05

Ponml


People also ask

Which formatting method's in Python3 allows multiple substitutions and value formatting?

Python's str. format() method of the string class allows you to do variable substitutions and value formatting. This lets you concatenate elements together within a string through positional formatting.

How do you use %s in Python?

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value.

What does {: 2f mean in Python?

2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.

What is %d in Python3?

The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values. Python3.


2 Answers

As ninjagecko suggested, you want to construct your format string in pieces.

Using implicit field numbering as I have done helps simplify this, although it isn't strictly necessary (explicit numbering just gets a little more verbose making sure the numbers line up). Mixing old- and new-style string formatting also means we can skip some tedious escaping of special characters.

subfmt = ", ".join(["{:5d}"]*3)
fmt = "{:1}{:1}, {:3d}, %s, {:8.3F}, {:>7}" % subfmt
tester = ["M", "T", 1111, 2222, 3333, 4444, 234.23456, "testing"]

>>> print(fmt)
{:1}{:1}, {:3d}, {:5d}, {:5d}, {:5d}, {:8.3F}, {:>7}
>>> print(fmt.format(*tester))
MT, 1111,  2222,  3333,  4444,  234.235, testing
like image 89
ncoghlan Avatar answered Oct 10 '22 01:10

ncoghlan


You could generate part of your format string like so (adapt as you see fit):

>>> ','.join(['%s']*5)
'%s,%s,%s,%s,%s'

>>> ','.join(['%i']*5) % (1,2,3,4,5)
'1,2,3,4,5'

or even like '%i '*10 (more terse, if you don't mind an extra separator value at the end)

edit: the above is deprecated, and the original poster would like an explicit example, so here is an explicit example with str.format ( http://docs.python.org/library/stdtypes.html#str.format ) using the format specified in http://docs.python.org/library/string.html#formatstrings . Note that you can do much more complicated things than this demo, such as using nice keyword arguments or even object attributes. See docs.

tester = ["M", "T", 1111, 2222, 3333, 4444, 234.23456, "testing"]
fmt = "{}{}, {:3d}, " + 3*"{:5d}, " + "{:8.3F}, {:>7}"
fmt.format(*tester)

result:

'MT, 1111,  2222,  3333,  4444,  234.235, testing'

(edit2: with apologies to ncoghlan; I did not see his new answer before updating)

like image 34
ninjagecko Avatar answered Oct 10 '22 02:10

ninjagecko