ValueError:格式为python的零长度字段名称 [英] ValueError: zero length field name in format python

查看:192
本文介绍了ValueError:格式为python的零长度字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:
ValueError:零长度的字段名称的格式 Python 3.0,3.1,3.2错误


我花了数小时试图解决这个问题却无济于事。我阅读了本指南
我还没有找到任何示例如何做我需要的。



当我运行这个脚本的时候,我得到了这个错误(部分省略):


pre> <... c $ c> Traceback(...):

output.write({:&026,.18e} {:> 026,.18e} \\ \\ n.format(x,y))
ValueError:格式中的零长度字段名称。

代码是用python 2.6或2.7编写的,但我运行python 3.1。我怎么需要改变输出格式,以便它能工作?

  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,N + 2):
output.write({:> 026,.18e} {:> 026 ,. 1#zac.pogoj

。 18e} \\\
.format(x,y))
y + = h * f(x,y)
x = i * h
output.close()

感谢您的帮助。

解决方案

很可能你运行的是旧的Python版本,而不是3.1。在Python 2.6中,您需要格式规范的索引,如下所示:

 {0} {1} \\\
.format(x,y)

更新你的Python版本到最近的版本,最好是2.7或3.2,解决问题。根据文档,忽略数字索引应该在Python 3.1中工作
$ b


在版本3.1中更改:位置参数说明符可以省略,所以{} {}等同于'{0} {1}'。



Possible Duplicate:
“ValueError: zero length field name in format” error in 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}\n".format(x,y))
ValueError: zero length field name in format.

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}\n".format(x,y))
        y += h*f(x,y)
        x = i*h
    output.close()

Thanks for your help.

解决方案

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}\n".format(x,y)

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:

Changed in version 3.1: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.

这篇关于ValueError:格式为python的零长度字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆