Pandas无法从pycharm中的文件路径读取,但在Jupiter中工作正常

时间:2018-02-01 21:40:19

标签: python python-3.x pandas pycharm jupyter-notebook

有没有人知道为什么每当我尝试在pycharm中加载pd.read_excel文件时遇到奇怪的错误,当它在Jupyter笔记本中工作正常时?

这是错误:

-------------------------------------------------------------------------------
ERROR in app [/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py:1560]:
Exception on /catalogue/etl/ [GET]
--------------------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_restplus/api.py", line 313, in wrapper
    resp = resource(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/Users/anant/project/app/resources/catalogueController.py", line 18, in get
    catalogabc.transform()
  File "/Users/anant/project/app/models/catalog_abc_ETL.py", line 55, in transform
    self.extractPageAndConvertToCsv(each_file)
  File "/Users/anant/project/app/models/catalog_abc_ETL.py", line 98, in extractPageAndConvertToCsv
    sheet = pd.read_excel(io=filename, sheetname=pageNumber)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/util/_decorators.py", line 118, in wrapper
    return func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/excel.py", line 230, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/excel.py", line 285, in __init__
    io, _, _ = get_filepath_or_buffer(self._io)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/common.py", line 211, in get_filepath_or_buffer
    raise ValueError(msg.format(_type=type(filepath_or_buffer)))
ValueError: Invalid file path or buffer object type: <class 'app.models.catalog_abc_ETL.Catalogue_abc'>

以下是代码:

    def load(self):
        import os
        list_of_files = os.listdir(os.getcwd())
        for each_file in list_of_files:
            if each_file.startswith('temp') and each_file.endswith(
                    '.csv'):  # since its all type str you can simply use startswith
                print(each_file)
                self.uploadTransformedCSV(each_file)

 def extractPageAndConvertToCsv(filename, delimiter=';', pageNumber=1):
        #df = pd.ExcelFile(filename)
        #sheet = df.parse(pageNumber)
        sheet = pd.read_excel(io=filename, sheetname=pageNumber)
        sheet = sheet[sheet.columns[0:2]]
        sheet.insert(loc=0, column='feed_time_stamp', value=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
        sheet.to_csv(filename + ".csv", sep=delimiter, index=False)

P.S。这在Jupiter笔记本中工作正常但不是pycharm

0 个答案:

没有答案