Python: 字符串格式化时未全部转换参数

0 投票
2 回答
697 浏览
提问于 2025-04-17 23:19
import datetime

now = datetime.date.today()
now = str(now)
index = open('/var/www/index.html', 'a')
index.write('<br> $s,' %now)
index.close()

我一直收到这个错误。

2 个回答

1
index.write('<br> {now},'.format(now=now))

.formatstring-formatting 好得多,具体可以参考 这个链接

2

你需要使用正确的格式:

index.write('<br> %s,' %now)

应该是 %s,而不是 $s

撰写回答