Flask TypeError: 预期的字节

huangapple go评论100阅读模式
英文:

Flask TypeError: Expected bytes

问题

我正在尝试从表格中获取数据并响应结果。我遇到了这个问题,因为数据来自数据库。以下是代码:

  1. cursor = connection.cursor()
  2. cursor.execute("select * from table")
  3. result = cursor.fetchall()
  4. for row in result:
  5. data = row
  6. connection.close()
  7. return Response(
  8. data,
  9. mimetype="text/csv",
  10. headers={
  11. "Content-disposition": "attachment; filename=data.csv"
  12. },
  13. )

在尝试获取数据时,我遇到了这个错误:

  1. TypeError: 期望的是字节

我该如何解决这个问题?

英文:

I'm trying to get data from a table and response the result. I am facing this issue since the data is from a database. Below is the code:

  1. cursor = connection.cursor()
  2. cursor.execute("""select * from table""")
  3. result = cursor.fetchall()
  4. for row in result:
  5. data = row
  6. connection.close()
  7. return Response(
  8. data,
  9. mimetype="text/csv",
  10. headers={
  11. "Content-disposition": "attachment; filename=data.csv"
  12. },
  13. )

While trying to get it, I am getting this error:

  1. TypeError: Expected bytes

How can I resolve this?

答案1

得分: 1

以下是翻译好的代码部分,不包括翻译请求的内容:

  1. import csv
  2. from io import StringIO
  3. # ...
  4. # 实例化一个StringIO来在内存中缓冲CSV
  5. sio = StringIO()
  6. # 实例化一个CSV写入器来写入StringIO
  7. cw = csv.writer(sio)
  8. # 执行查询
  9. cursor.execute("""select * from table""")
  10. # 根据您的数据库,您可以在循环之前使用 `cw.writerow`
  11. # 来将列名作为第一行写入CSV。
  12. # 循环遍历查询返回的行,将每个单元格转换为字符串后写入CSV写入器。
  13. # 您可能需要调整 `str()` 调用以特定方式格式化日期等内容。
  14. for row in cursor:
  15. cw.writerow([str(cell) for cell in row])
  16. # 获取StringIO的内容并编码为UTF-8字节
  17. csv_bytes = sio.getvalue().encode("utf-8")
  18. return Response(
  19. csv_bytes,
  20. mimetype="text/csv",
  21. headers={
  22. "Content-disposition": "attachment; filename=data.csv"
  23. },
  24. )
英文:

You're probably looking for something like this. I elided the connection-related bits, because they're specific to your database connection.

  1. import csv
  2. from io import StringIO
  3. # ...
  4. # Instantiate a StringIO to buffer the CSV in memory to
  5. sio = StringIO()
  6. # Instantiate a CSV writer to write to the StringIO
  7. cw = csv.writer(sio)
  8. # Execute the query
  9. cursor.execute("""select * from table""")
  10. # Depending on your database, you could use `cw.writerow`
  11. # before the loop to write the column names as the first line here.
  12. # Loop over the rows returned by the query, casting each cell to a string
  13. # before writing to the CSV writer. You may need to adjust the `str()`
  14. # invocation to format e.g. dates in a specific way.
  15. for row in cursor:
  16. cw.writerow([str(cell) for cell in row])
  17. # Get the StringIO's contents and encode to UTF-8 bytes
  18. csv_bytes = sio.getvalue().encode("utf-8")
  19. return Response(
  20. csv_bytes,
  21. mimetype="text/csv",
  22. headers={
  23. "Content-disposition": "attachment; filename=data.csv"
  24. },
  25. )

huangapple
  • 本文由 发表于 2023年3月7日 18:27:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定