简体   繁体   中英

Why Python returns error “a float is required” while writing to file?

I'm trying to create files with id from database and insert some text, but it still don't want to let me do it.

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))

If i put %(title) after write function, it returns unsupported operand type(s) for %: 'int' and 'str'

When using old-style string formatting, "%" has a given meaning, so you have to escape it (with itself) if you want a litteral "%" in your format string, ie:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM