我在输出格式化字符串时遇到错误。我使用的是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))
我收到以下错误: ValueError:格式为
的零长度字段名称关于我做错的任何想法?
谢谢!
答案 0 :(得分:4)
Python 2.6及更早版本要求您指定格式化值的索引。这是一个例子:
'[{} {}]'.format(a, b) # Python 2.7 and later
'[{0} {1}]'.format(a, b) # Python 2.6 and earlier
我没有Python 2.6解释器,但我相信这是您需要的语法:
site_file.write('[{0:8.2f}{1:8.2f}]'.format(lat,long))