在字符串格式化期间,并非所有参数都已转换 [英] Not all of arguments converted during string formatting

查看:552
本文介绍了在字符串格式化期间,并非所有参数都已转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,该脚本将当前日期和时间保存为文件名,但是我收到一条错误消息,指出 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")

合法的做法是不传递任何格式参数,就像对<$ c所做的一样$ c>%s.sql ,但是传递参数但传递正确的值( w%(今天))是不合法的传递了一个,但是 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)

这篇关于在字符串格式化期间,并非所有参数都已转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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