无法在Python中格式化字符串

时间:2015-11-21 22:25:57

标签: python

我得到TypeError: not enough arguments for format string以获取以下代码:

 print("%s,%s,%s" % time.strftime("%m/%d/%Y %H:%M:%S"), (sensor.read_temperature(), (sensor.read_humidity()))

它应该以CSV格式将日期/时间和两个变量打印到控制台。我的格式字符串出了什么问题?

1 个答案:

答案 0 :(得分:3)

您只提供格式字符串一个参数;其他两个值是<table> <thead><tr><th>title</th><th>artist</th></tr></thead> <tbody> <tr><td>No Ordinary Morning</td><td>Chicane</td></tr> <tr><td>From Where I Stand</td><td>Chicane</td></tr> <tr><td>Come Back</td><td>Chicane</td></tr> <tr><td>Offshore</td><td>Chicane</td></tr> <tr><td>Don't Give Up (feat. Bryan Adams)</td><td>Chicane</td></tr> <tr><td>Spirit (Chicane Rework Mix) [feat. Jewel]</td><td>Chicane</td></tr> <tr><td>Halcyon</td><td>Chicane</td></tr> <tr><td>Sunstroke</td><td>Chicane</td></tr> <tr><td>Stoned In Love (feat. Tom Jones)</td><td>Chicane</td></tr> <tr><td>Wake Up (feat. Keane)</td><td>Chicane</td></tr> <tr><td>Come Tomorrow</td><td>Chicane</td></tr> <tr><td>Leaving Town (feat. Salt Tank)</td><td>Chicane</td></tr> </tbody> </table> <table> <thead><tr><th>title</th><th>artist</th></tr></thead> <tbody> <tr><td>No Ordinary Morning</td><td>Chicane</td></tr> <tr><td>From Where I Stand</td><td>Chicane</td></tr> <tr><td>Come Back</td><td>Chicane</td></tr> <tr><td>Offshore</td><td>Chicane</td></tr> <tr><td>Don't Give Up (feat. Bryan Adams)</td><td>Chicane</td></tr> <tr><td>Spirit (Chicane Rework Mix) [feat. Jewel]</td><td>Chicane</td></tr> <tr><td>Halcyon</td><td>Chicane</td></tr> <tr><td>Sunstroke</td><td>Chicane</td></tr> <tr><td>Stoned In Love (feat. Tom Jones)</td><td>Chicane</td></tr> <tr><td>Wake Up (feat. Keane)</td><td>Chicane</td></tr> <tr><td>Come Tomorrow</td><td>Chicane</td></tr> <tr><td>Leaving Town (feat. Salt Tank)</td><td>Chicane</td></tr> </tbody> </table> <table> <thead><tr><th>title</th><th>artist</th></tr></thead> <tbody> <tr><td>No Ordinary Morning</td><td>Chicane</td></tr> <tr><td>From Where I Stand</td><td>Chicane</td></tr> <tr><td>Come Back</td><td>Chicane</td></tr> <tr><td>Offshore</td><td>Chicane</td></tr> <tr><td>Don't Give Up (feat. Bryan Adams)</td><td>Chicane</td></tr> <tr><td>Spirit (Chicane Rework Mix) [feat. Jewel]</td><td>Chicane</td></tr> <tr><td>Halcyon</td><td>Chicane</td></tr> <tr><td>Sunstroke</td><td>Chicane</td></tr> <tr><td>Stoned In Love (feat. Tom Jones)</td><td>Chicane</td></tr> <tr><td>Wake Up (feat. Keane)</td><td>Chicane</td></tr> <tr><td>Come Tomorrow</td><td>Chicane</td></tr> <tr><td>Leaving Town (feat. Salt Tank)</td><td>Chicane</td></tr> </tbody> </table> <table> <thead><tr><th>title</th><th>artist</th></tr></thead> <tbody> <tr><td>No Ordinary Morning</td><td>Chicane</td></tr> <tr><td>From Where I Stand</td><td>Chicane</td></tr> <tr><td>Come Back</td><td>Chicane</td></tr> <tr><td>Offshore</td><td>Chicane</td></tr> <tr><td>Don't Give Up (feat. Bryan Adams)</td><td>Chicane</td></tr> <tr><td>Spirit (Chicane Rework Mix) [feat. Jewel]</td><td>Chicane</td></tr> <tr><td>Halcyon</td><td>Chicane</td></tr> <tr><td>Sunstroke</td><td>Chicane</td></tr> <tr><td>Stoned In Love (feat. Tom Jones)</td><td>Chicane</td></tr> <tr><td>Wake Up (feat. Keane)</td><td>Chicane</td></tr> <tr><td>Come Tomorrow</td><td>Chicane</td></tr> <tr><td>Leaving Town (feat. Salt Tank)</td><td>Chicane</td></tr> </tbody> </table>函数的单独参数。

移动左括号:

print()

现在要插值的所有值都在一个元组中,应用于print("%s,%s,%s" % ( time.strftime("%m/%d/%Y %H:%M:%S"), sensor.read_temperature(), sensor.read_humidity()) ) 部分。你可以在这里使用'...' %函数,将print()参数设置为逗号:

sep