mah*_*ood 4 python stdout python-3.x
是否可以传递多个参数sys.stdout.write?我看到的所有示例都使用一个参数.
以下陈述不正确.
sys.stdout.write("\r%d of %d" % read num_lines)
Syntax Error: sys.stdout.write
sys.stdout.write("\r%d of %d" % read, num_lines)
not enough arguments for format string
sys.stdout.write("\r%d of %d" % read, %num_lines)
Syntax Error: sys.stdout.write
sys.stdout.write("\r%d of %d" % read, num_lines)
not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
您需要将变量放在元组中:
>>> read=1
>>> num_lines=5
>>> sys.stdout.write("\r%d of %d" % (read,num_lines))
1 of 5>>>
Run Code Online (Sandbox Code Playgroud)
或使用str.format()方法:
>>> sys.stdout.write("\r{} of {}".format(read,num_lines))
1 of 5
Run Code Online (Sandbox Code Playgroud)
如果您的参数在iterable中,您可以使用解包操作将它们传递给string的format()属性.
In [18]: vars = [1, 2, 3]
In [19]: sys.stdout.write("{}-{}-{}".format(*vars))
1-2-3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6503 次 |
| 最近记录: |