2

In Fortran, I can use:

open(10, file = 'output')
write(10, '(5A10)') 'apple', 'banana', 'orange', 'grape', 'berry'

Is there a similar way in Python of writing the output?

I know I can use:

f.write('%5s\t%5s\t%5s\t%5s\t%5s' % ('apple', 'banana', 'orange', 'grape', 'berry'))

But how can I make it more compact? Using a similar way of '(5A10)', instead of the lengthy '%5s\t%5s\t%5s\t%5s\t%5s'.

4
  • You can try storing it as a tuple, say t, and use f.write(*t). Commented Mar 13, 2016 at 23:07
  • It's not just Fortran 90 where you can do this ... This formatting goes back to at least Fortran 77. I never really dug into the F66 standard well enough to know whether you could do it portably there... Commented Mar 13, 2016 at 23:13
  • note modern fortran supports a variable multiplier: write(10,'(*(a10))') so you don't need to hard code the list length. I don't think there is a nice way to do that in python. Commented Mar 14, 2016 at 15:16
  • @agentp: Great tip! Works like a charm. Thanks a lot! I should ask more questions in Stack Overflow. People here are like genius! Lol Commented Mar 16, 2016 at 6:34

2 Answers 2

7

You can use fortranformat: https://pypi.python.org/pypi/fortranformat

Works great!

>>> import fortranformat as ff
>>> header_line = ff.FortranRecordWriter('(A15, A15, A15)')
>>> header_line.write(['x', 'y', 'z'])
  '              x              y              z'
>>> line = FortranRecordWriter('(3F15.3)')
>>> line.write([1.0, 0.0, 0.5])
  '          1.000          0.000          0.500'
>>> line.write([1.1, 0.1, 0.6])
  '          1.100          0.100          0.600'
Sign up to request clarification or add additional context in comments.

1 Comment

'line' is string, so, you can print or write to a file.
3

No, there's no real shortcut here. In this case, you might try:

file_handle.write('\t'.join('%s' % f for f in fruits))

I'm not sure that this ends up being much more compact until fruits gets a little longer, but I think that it does make the output a little more obvious...

Also, if I'm reading your format statement correctly (it's been 2 years since I've spend much time with Fortran), you are formatting 5 strings with each 10 characters wide. The corresponding python format code for a string that is 10 characters wide is '%10.10s' (10 characters minimum, 10 characters maximum width). In that case, the tabs are unnecessary and you could write it as:

>>> fruits = ('apple', 'banana', 'orange', 'grape', 'berry')
>>> (5*'%10.10s') % fruits
'     apple    banana    orange     grape     berry'

Which isn't too much more verbose than the fortran way.

4 Comments

OK. Because the data I am dealing with has a varying column. The fortran format writing is more easier to deal with. I will use f.write('%s' % ('apple\tbanana\torange\tgrape\tberry')) for now. But I just learnt a new way of writing output in Python from you. Already keep a note for this :)
@JianliCheng -- '%s' % 'some_string' is the same thing as some_string. No need to do formatting in that case :-)
The 5*'%10.10s' trick is exactly what I am looking for. Thank you so much. But what if some elements in my array are integers or floats? In fortran, I can do '(2A4, 3I4)', which write 2 strings with 4 characters and 3 integers with 4 characters.
'2 * '%4.4s' + 3 * '%4.4d' is one option. This will 0-pad the integers. If you don't want them 0-padded (as I don't think they will be in fortran with that format statement), then you could just do 5 * '%4.4s' as the format statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.