Honza Sedloň
Honza Sedloň

Reputation: 374

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'

Upvotes: -1

Views: 119

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

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

Upvotes: 1

Related Questions