【问题标题】:Atomically read from excel (for luigi workflow)从 excel 原子读取(用于 luigi 工作流程)
【发布时间】:2018-10-08 00:31:02
【问题描述】:

我正在尝试使用 pandas.read_excel() 使用内置(原子)luigi 方法在我的 Luigi 工作流程中打开一个 excel 文件。

如果self.input() 是我的 excel 文档的 luigi 目标,我想做类似的事情:

with self.input().open('r') as f: pandas.read_excel(f)

或更笼统地说:

with open(filename) as f: pandas.read_excel(f)

但是,这给了我一个错误: *** UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 10: invalid continuation byte

免责声明:

excel 文件来自外部任务,所以我无法控制它是在什么类型的计算机上制作的,或者它是否包含 NA 或空白单元格。

【问题讨论】:

  • 看起来您的输入文件有一个不可解码的字符。尝试在excel上查看文件并清理它。否则请查看上一条评论的链接:)
  • 运行:self.input().open('r') as f: pd.read_excel(f, encoding = sys.getfilesystemencoding()) 无法解决问题
  • @Laura 如果 excel 表是在 Windows 机器上生成的,它不会在您的文件系统的编码中。

标签: python pandas luigi


【解决方案1】:

问题是我的 self.input()(指向保存我的 excel 文件的位置)应该使用 format = Nop。我的 luigi 目标应该返回如下内容:

luigi.LocalTarget('excelfile.xlsx', format=luigi.format.Nop)

有了这个目标定义,我可以使用原子读取:

with self.input().open() as f:
    df = pd.read_excel(f)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 2014-03-24
  • 2011-05-17
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多