1

I would like to format some text to the protein data bank file format. Basically what I need to do is start the text in specific columns. Right now I'm doing the formatting using ljust and rjust but I feel like there should be some magic function that I could use give a word and in what column it should start.

Any help would be greatly appreciated. Thanks

If my Statckoverflow searching failed, please point me in that direction.

1
  • show what you have and the formatting you want... Commented Sep 3, 2011 at 20:31

2 Answers 2

1

Here is an example of how one might format a "Coordinate Record Description" using str.format:

CRD='{r:<6}{a:>9.3f}{b:>9.3f}{c:>9.3f}{alpha:7.2f}{beta:7.2f}{gamma:7.2f} {sp:<11}{z:>4}'

data=dict(r='CRYST1',a=117,b=15.0,c=39,alpha=90,beta=90,gamma=90,sp='P 21 21 21',z=8)
print(CRD.format(**data))

# CRYST1  117.000   15.000   39.000  90.00  90.00  90.00 P 21 21 21    8

The format specifier CRD is interpreted this way:

{r:<6} tells format to string-interpolate the value of str(r), left justified, with width=6.

{a:>9.3f} tells format to string-interpolate the float value of a, right justified, with total width 9 and 3 digits after the decimal point.

The complete description of how the format specified works is given here.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use string "format" function with alignment. http://docs.python.org/library/string.html#formatspec Example with columns you can find in source codes to the 'Programming in Python 3: A Complete Introduction to the Python Language' by Mark Summerfield, file print_unicode.py

Comments

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.