相关疑难解决方法(0)

ValueError:格式为python的零长度字段名称

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

我花了好几个小时试图解决这个问题,但无济于事.我读了这本指南.我没有找到如何做我需要的任何例子.

当我运行脚本时,我收到此错误(部分省略):

Traceback (...):
   [...]
   output.write("{: > 026,.18e} {: > 026,.18e}\n".format(x,y))
ValueError: zero length field name in format.
Run Code Online (Sandbox Code Playgroud)

代码是用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 #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()
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

python format

60
推荐指数
1
解决办法
7万
查看次数

Python字符串格式化-字符串替换后的百分比符号

我试图通过提供从这样的方法中获取的参数来格式化 python 中的 SQL 查询

query = "select * from employees where employee_name like '%s%'" % (name)
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,出现以下错误(python 2.6)

ValueError: unsupported format character ''' (0x27) at index 886
Run Code Online (Sandbox Code Playgroud)

我也在命令行中尝试了这个来找出问题

>>> print "hello '%s'" % ('world')
hello 'world'
>>> print "hello '%s\%'" % ('world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> print "hello '%s%'" % ('world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
2121
查看次数

标签 统计

python ×2

format ×1