我有一個生成 CSV 的生成器函式,根據Flask 的檔案,我將該函式傳遞到 Response 物件中。
app.route("/stream")
...
def generate():
yield ",".join(result.keys()) #Header
for row in result:
yield ",".join(["" if v is None else str(v) for v in row])
return current_app.response_class(stream_with_context(generate()))
當我去閱讀我里面的回應時,Request我得到的回應是一個巨大的字串,而不是逐行獲取,因此可以將其寫入csvwriter
s = requests.Session()
with s.get(
urllib.parse.urljoin(str(current_app.config["API_HOST_NAME"]), str("/stream")),
headers=None,
stream=True,
) as resp:
for line in resp.iter_lines():
if line:
print(line) #All the rows are concatenated in one big giant String
cw.writerow(str(line, "utf-8").split(","))
uj5u.com熱心網友回復:
正如您在發布的檔案中看到的那樣:
yield f"{','.join(row)}\n" # <=== Newline at the end!
新行在 yield 陳述句中提供。如果你不換行,你將不會在回應中得到一個。只需添加它 '\n'。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/463599.html
標籤:python-3.x 休息 http 烧瓶
