0

I am encountering an error in outputting a formatted string. I am using Python2.6

lat = 23.456
long = 34.129
# I want to output lat and long, both occupying 8 columns in width, right justified and with 2 values beyond the decimal point
site_file = open('test.site','w')
site_file.write('[{:8.2f}{:8.2f}]'.format(lat,long))

I get the following error: ValueError: zero length field name in format

Any ideas on what I am doing wrong?

thanks!

2
  • 2
    long is a buit-in function for converting to a long integer. Please don't use it as a variable name. Commented Mar 17, 2014 at 22:06
  • 1
    The code you posted works for me - v2.7. Commented Mar 17, 2014 at 22:10

1 Answer 1

4

Python 2.6 and earlier require that you specify the index of the formatted values. Here is an example:

'[{} {}]'.format(a, b)    # Python 2.7 and later 
'[{0} {1}]'.format(a, b)  # Python 2.6 and earlier

I do not have a Python 2.6 interpreter handy, but I believe this is the syntax that you need:

site_file.write('[{0:8.2f}{1:8.2f}]'.format(lat,long))
Sign up to request clarification or add additional context in comments.

1 Comment

From the docs for 2.6 Each replacement field contains either the numeric index of a positional argument, or the name of a keyword argument.

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.