【问题标题】:input Csv models.FileField into pandas object将 Csv models.FileField 输入到 pandas 对象中
【发布时间】:2020-06-12 09:05:42
【问题描述】:

我有模型字段实例。

myCsv =  myFile.objects.get(id=1) // myCsv.document is models.FileField 

并且想把它放在熊猫中。

df = pd.read_csv(thred.document.read())

OSError: Expected file path name or file-like object, got <class 'bytes'> type

如何将 cev 文件放入 pandas 对象??

【问题讨论】:

  • 试试df = pd.read_csv(thred.document)

标签: python django csv


【解决方案1】:

或许你可以这样尝试:

thred =  myFile.objects.get(id=1)
df = pd.read_csv(thred.document.path)

有关路径的更多信息,请参阅documentation,了解如何管理FileField(或ImageField)中的文件。

【讨论】:

  • df = pd.read_csv(myCsv.document.path()) 下方有错误TypeError: 'str' object is not callable
  • 我没有在这个答案中打电话给path()。只是path
  • FileNotFoundError: [Errno 2] File b'/media/documents/threshold_1_eTqYWZf.csv' does not exist: b'/media/documents/threshold_1_eTqYWZf.csv'
  • 快到了.... /media/documents/threshold_1_eTqYWZf.csv'...but can't open.. b' 里面有一个文件是相关的???
  • 我认为这不是问题所在。请确认文件是否存在。
【解决方案2】:

以下是对我有用的。 thred - django 模型的对象。 文档 - 文件域

import pandas as pd
csv_reader = pd.read_csv(thred.document.open().url)

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 2017-04-15
    • 2020-07-01
    • 2018-06-12
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多