Python:不是在字符串格式化时转换的所有参数 [英] Python: Not all of arguments converted during string formatting
问题描述
我正在写一个脚本,将当前的日期和时间保存为一个文件名,但我得到一个错误,指出TypeError:不是所有在字符串格式转换的参数我是Python新手,可能错过了一些明显的东西。代码如下:
Im wrtiting a script which saves the current date and time as a filename but I get an error stating "TypeError: not all arguments converted during string formatting" I am new to Python andmay of missed something obvious. Code below:
from subprocess import Popen
import datetime
today = datetime.date.today()
today = str(today)
print today
f = open("%s.sql", "w" % (today))
x = Popen(["mysqldump", "-u", "root", "-pucsdrv", "normalisationtion"], stdout = f)
x.wait()
f.close()
推荐答案
字符串格式错误的地方;它需要正确的格式化后的字符串:
You're putting the string formatting in the wrong place; it needs to be right after the string that's being formatted:
f = open("%s.sql" % (today), "w")
合法的是不传递格式参数,就像%s.sql
,但传递参数并不合法(w%(today)
传递一个,但是在w
中没有字符串格式,所以你得到一个错误,并不是所有的参数都被使用了)
It's legal to not pass any formatting arguments, like you did with "%s.sql"
, but it's not legal to pass arguments but not the right amount ("w" % (today)
passes one, but there's no string formatting in "w"
, so you get an error that not all of the arguments were used)
这篇关于Python:不是在字符串格式化时转换的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!