多个 SQL 行的 f 字符串
f-string for multiple SQL lines
这个有效(只有一行):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
但是这些没有(2 行):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database
WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
c.execute("SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
如何在多行中使用 f-string?
这个问题源于本质上无效的多行,而不是 f-string.. Note that you only need to use your variables inside the {}
parenthesis and not pass them separately with f-string
syntax. Use triple quotes or backslashes at end of strings to use multi line strings. Link to docs
的真正错误
test = "this is a
bad multiline " #Raises SyntaxError: EOL while scanning string literal
test = ''' this is a
valid multiline'''
a = "answer"
print(f'''test
is complete. check {a} ''')
这个有效(只有一行):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
但是这些没有(2 行):
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database
WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))
c.execute(f"SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
c.execute("SELECT Firm, Platform, `Sale in million` FROM database"
f"WHERE Platform IN ({','.join('?' for _ in list_table)}) ORDER BY Firm", (list_table))"
如何在多行中使用 f-string?
这个问题源于本质上无效的多行,而不是 f-string.. Note that you only need to use your variables inside the {}
parenthesis and not pass them separately with f-string
syntax. Use triple quotes or backslashes at end of strings to use multi line strings. Link to docs
test = "this is a
bad multiline " #Raises SyntaxError: EOL while scanning string literal
test = ''' this is a
valid multiline'''
a = "answer"
print(f'''test
is complete. check {a} ''')