为什么 Python 在写入文件时返回错误"a float is required"?



我正在尝试从数据库创建带有id的文件并插入一些文本,但它仍然不想让我这样做。

self.a.execute(
    """INSERT INTO serial (serial.Name, Description, GenreID, CreationDate) VALUES (%s, %s, %s, %s)""",
       (title, overview, genre, release_date))
self.a.execute("""SELECT id, serial.Name FROM serial WHERE Name=%s""", title)
title = str(self.a.fetchall()[0]['id'])
with open("templates/serials/" + title + '.html', 'w+') as o:
o.write("""
        {% extends '../base.html' %}
        {% block content %}
        <p>%s</p>
        {% endblock %}
        """ % (title))

如果我把%(title)放在写函数之后,它会返回unsupported operand type(s) for %: 'int' and 'str'

当使用旧式字符串格式时,"%"具有给定的含义,因此如果您希望在格式字符串中使用乱码"%",则必须对其进行转义(与自身(,即:

o.write("""
    {%% extends '../base.html' %%}
    {%% block content %%}
    <p>%s</p>
    {%% endblock %%}
    """ % (title))

最新更新