Python 语法错误:f 字符串:不匹配的“[”

Ben*_*ber 1 python fastapi

from fastapi import FastAPI
from fastapi.params import Body

app = FastAPI()

@app.post("/createposts")
def create_posts(payload: dict = Body(...)):
    print(payload)
    return {"new_post" : f"title {payload["title"]} content: {payload["content"]}"}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 Fastapi 创建 API,但每次运行代码时都会收到与 return 语句相关的错误: SyntaxError: f-string: unmatched '['

谢谢你!

Evg*_*kov 5

请更换

return {"new_post" : f"title {payload["title"]} content: {payload["content"]}"}

return {"new_post" : f"title {payload['title']} content: {payload['content']}"}

"里面不能有引号f"..."

该错误表明,在第一个字符串之后,[字符串停止并断开。