excel - 使用python写入HDFS中的excel文件

标签 excel hdfs openpyxl apache-zeppelin pandas.excelwriter

尝试使用 zeppelin 笔记本在 HDFS 上使用 Openpyxl 将数据帧写入 excel 工作簿。

我的代码如下 -

with pd.ExcelWriter('hdfs:///ip/docs/excel_files/ds.xlsx', engine="openpyxl", mode="w") as writer:
    df.to_excel(writer, sheet_name='Sheet3') 

错误 -
Fail to execute line 2:      df.to_excel(writer, sheet_name='Sheet3')
Traceback (most recent call last):
  File "/tmp/zeppelin_pyspark-8199079202247699001.py", line 380, in <module>
    exec(code, _zcUserQueryNameSpace)
  File "<stdin>", line 2, in <module>
  File "/opt/anaconda3/lib/python3.7/site-packages/pandas/io/excel.py", line 1191, in __exit__
    self.close()
  File "/opt/anaconda3/lib/python3.7/site-packages/pandas/io/excel.py", line 1195, in close
    return self.save()
  File "/opt/anaconda3/lib/python3.7/site-packages/pandas/io/excel.py", line 1228, in save
    return self.book.save(self.path)
  File "/opt/anaconda3/lib/python3.7/site-packages/openpyxl/workbook/workbook.py", line 396, in save
    save_workbook(self, filename)
  File "/opt/anaconda3/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 292, in save_workbook
    archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
  File "/opt/anaconda3/lib/python3.7/zipfile.py", line 1204, in __init__
    self.fp = io.open(file, filemode)

FileNotFoundError: [Errno 2] No such file or directory: 'hdfs:///ip/docs/excel_files/ds.xlsx'

最佳答案

如果您不在 Windows 上,请尝试使用 pydoop,如下所示:

import pydoop.hdfs as pydoop_hdfs

with pydoop_hdfs.open('hdfs://path_to_file/file.xlsx', mode='wb') as f:
    with pd.ExcelWriter(f, engine='xlsxwriter') as writer:
        df.to_excel(writer, sheet_name='Sheet')
        writer.save()

关于excel - 使用python写入HDFS中的excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60043265/

相关文章:

vba - 需要错误 424 对象

hadoop - 使用HDFS代替spark.local.dir

docker - Hadoop在不使用IP的情况下无法访问数据节点

c# - comObject 可以转换为 Excel.ApplicationClass 吗?

excel - 类型不匹配错误,Nothing 且循环未终止

hadoop - HUE共享工作流程作业的权限问题

python - 如何使用 Python 读取包含扩展字体的 Excel 文件? (openpyxl 错误 : Max value is 14)

python - 在 openpyxl load_workbook、use_iterators 中设置值

python - 没有名为 'openpyxl' 的模块 - Python 3.4 - Ubuntu

excel - 按变量引用表列