IOError: [Errno 2] No such file or directory: '~/adx_stat/files/excel/dsp_report/2020101.xls'

刘超 6天前 ⋅ 47 阅读   编辑

一、描述

  python2.7.6 pandas使用to_excel方法生成excel文件,有如下提示

>>> import pandas as pd
>>> df1 = pd.DataFrame([['Snow','M',22],['Tyrion','M',32],['Sansa','F',18],['Arya','F',14]], columns=['name','gender','age'])
>>> df1.to_excel("~/adx_stat/files/excel/dsp_report/2020101.xls")
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1766, in to_excel
    engine=engine)
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/formats/excel.py", line 654, in write
    writer.save()
  File "/usr/local/lib/python2.7/dist-packages/pandas/io/excel.py", line 1462, in save
    return self.book.save(self.path)
  File "/home/sdev/.local/lib/python2.7/site-packages/xlwt/Workbook.py", line 710, in save
    doc.save(filename_or_stream, self.get_biff_data())
  File "/home/sdev/.local/lib/python2.7/site-packages/xlwt/CompoundDoc.py", line 262, in save
    f = open(file_name_or_filelike_obj, 'w+b')
IOError: [Errno 2] No such file or directory: '~/adx_stat/files/excel/dsp_report/2020101.xls'
>>> 

二、解决方法

  to_excel识别不了~(当前用户的家目录),改成绝对地址就可以了

>>> df1.to_excel("/home/sdev/adx_stat/files/excel/dsp_report/2020101.xls")

注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: