为什么Python在写入文件时返回错误“需要浮点数”?(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) ,则返回unsupported operand type(s) for %: 'int' and 'str'
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'
最满意答案
使用旧式字符串格式时,“%”具有给定的含义,因此如果您希望格式字符串中有一个“%”,则必须将其转义(自身),即:
o.write(""" {%% extends '../base.html' %%} {%% block content %%} <p>%s</p> {%% endblock %%} """ % (title))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))更多推荐
发布评论