Python字符串格式设置为;写下;
我有一些这样的代码:Python字符串格式设置为;写下;,python,Python,我有一些这样的代码: #w = open("blabla.py", "w") is already called w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % textvalue,buttoncommand,str(rowvalue),str(columnvalue)) 但是,如果运行此操作,将出现以下错误: TypeError: not enough arguments for format s
#w = open("blabla.py", "w") is already called
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % textvalue,buttoncommand,str(rowvalue),str(columnvalue))
但是,如果运行此操作,将出现以下错误:
TypeError: not enough arguments for format string
怎么了?将变量包含到元组中:
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))
或者使用更好的格式版本:
w.write("Button(root, text = {0},"
"command={1}).grid(row={2},"
"column={3})\n".format(textvalue,
buttoncommand,
str(rowvalue),
str(columnvalue)))
将变量包含到元组中:
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))
或者使用更好的格式版本:
w.write("Button(root, text = {0},"
"command={1}).grid(row={2},"
"column={3})\n".format(textvalue,
buttoncommand,
str(rowvalue),
str(columnvalue)))
您需要将格式参数放入元组(添加括号):
…%(textvalue、buttoncommand、str(rowvalue)、str(columnvalue))
但是格式化字符串的%
语法已经过时。试试这个:
“{0}'、{1}'、{2}'、{3}'、{4}'.format(textvalue、buttoncommand、str(rowvalue)、str(columnvalue))
您需要将格式参数放入元组(添加括号):
…%(textvalue、buttoncommand、str(rowvalue)、str(columnvalue))
但是格式化字符串的%
语法已经过时。试试这个:
“{0}'、{1}'、{2}'、{3}'、{4}'.format(textvalue、buttoncommand、str(rowvalue)、str(columnvalue))
如果向格式字符串传递多个参数,则需要将它们放在括号中:
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))
^ ^
您可能还想看看新的格式语法:如果将多个参数传递给格式字符串,则需要将它们放在括号中:
w.write("Button(root, text = %s,command=%s).grid(row=%s,column=%s)\n" % (textvalue,buttoncommand,str(rowvalue),str(columnvalue)))
^ ^
您可能还想看看新的格式语法:yes。它们都被指定为值。是的。它们都被分配了值。谢谢。我还将尝试.format()版本。谢谢。我还将尝试.format()版本。