为什么写入文件时 Python returns 错误 "a float is required"?
Why Python returns error "a float is required" while writing to file?
我正在尝试使用数据库中的 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)
放在 write 函数之后,它 returns unsupported operand type(s) for %: 'int' and 'str'
当使用 old-style 字符串格式时,“%”具有给定的含义,因此如果您想在格式字符串中使用乱码的“%”,则必须将其(与其自身一起)转义,即:
o.write("""
{%% extends '../base.html' %%}
{%% block content %%}
<p>%s</p>
{%% endblock %%}
""" % (title))
我正在尝试使用数据库中的 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)
放在 write 函数之后,它 returns unsupported operand type(s) for %: 'int' and 'str'
当使用 old-style 字符串格式时,“%”具有给定的含义,因此如果您想在格式字符串中使用乱码的“%”,则必须将其(与其自身一起)转义,即:
o.write("""
{%% extends '../base.html' %%}
{%% block content %%}
<p>%s</p>
{%% endblock %%}
""" % (title))