ValueError:python格式的零长度字段名称
问题描述
可能重复:
“ValueError: 格式中的零长度字段名称”Python 3.0,3.1,3.2 中的错误
我花了几个小时试图解决这个问题,但无济于事.我阅读了本指南.我还没有找到任何示例如何做我需要的.
I have spent hours trying to solve this problem but to no avail. I read this guide. I haven't found any examples how to do what I need.
运行脚本时出现此错误(部分省略):
When I run the script I get this error (partly omitted):
Traceback (...):
[...]
output.write("{: > 026,.18e} {: > 026,.18e}
".format(x,y))
ValueError: zero length field name in format.
代码是用 python 2.6 或 2.7 编写的,但我运行的是 python 3.1.我需要如何更改输出格式才能正常工作?
The code is written in python 2.6 or 2.7 but I run python 3.1. How would I need to change output format so that it would work?
def f(x,y,a = 0.01):
return y/(a+x)-y**3
def ekspEuler(N,dat):
output = open(dat,"w")
h = 3.0/N
x,y = 0,1 #zac.pogoj
for i in range(1,N+2):
output.write("{: > 026,.18e} {: > 026,.18e}
".format(x,y))
y += h*f(x,y)
x = i*h
output.close()
感谢您的帮助.
解决方案
您可能运行的是旧的 Python 版本,而不是 3.1.在 Python 2.6 中,您需要格式规范中的索引,如下所示:
Chances are that you're running an old Python version, and not 3.1. In Python 2.6, you need indices in the format specs, like this:
"{0} {1}
".format(x,y)
将您的 Python 版本更新到最新版本,最好是 2.7 或 3.2,以解决问题.根据文档,省略数字索引 应该在 Python 3.1 中工作:
Update your Python version to a recent one, preferably 2.7 or 3.2, to fix the problem. According to the documentation, leaving out the numeric indices should work in Python 3.1:
3.1 版更改:位置参数说明符可以省略,因此{} {}"等同于{0} {1}".
Changed in version 3.1: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.
相关文章