提问者:小点点

熊猫从12个excel文档导入-FileNotFoundError:[Errno 2]没有这样的文件或目录:“x”


Python新手——我试图通过搜索上述错误时出现的解决方案进行反复试验。每当我在过去使用熊猫时,我都会在打开一个新文件向我的数据框添加信息之前使用虽然循环来遍历每个单独的文件。在这里,我的目标只是通过pdr.over_ride()从不同部门获得上周的数据。

它会正确下载文件,但当尝试将列指定为变量时(当我通过while循环一次指定一个列时有效),我会一直收到FileNotFoundError:[Errno 2]没有这样的文件或目录:“x”错误。

我不知道我在哪里要求python获取一个名为“x”的文件,我已经搜索了stackexchange,并试图通过pandas文档寻找解决方案。如果您能提供任何帮助,或找到解决方案或替代方法,我们将不胜感激。

这是完整的错误,然后是我的代码:

Traceback (most recent call last):
  File "c:/Users/jchil/Desktop/python/Finance/MarketWatch.py", line 45, in <module>
    df0 = pd.read_excel(savename[0], 'Sheet1')
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 824, in __init__
    self._reader = self._engines[engine](self._io)
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_xlrd.py", line 21, in __init__
    super().__init__(filepath_or_buffer)
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_xlrd.py", line 36, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "C:\Users\jchil\AppData\Local\Programs\Python\Python38-32\lib\site-packages\xlrd\__init__.py", line 111, in open_workbook
    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'x'
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
from matplotlib import style
from money import Money
import yfinance as yf
import pandas as pd
import numpy as np
import datetime
import sys
import re
import os

spiders = {
    '^GSPC': 'S&P 500', 'xlp': 'consumer staples select sector', 'xly': 'consumer discretionary select sector',
    'xlk': 'technology select sector', 'xlv': 'health care select sector', 'xli': 'industrials select sector',
    'xlu': 'utilities select sector', 'xlb': 'materials select sector', 'xle': 'energy select sector',
    'xlf': 'financial select sector', 'xlre': 'real estate select sector', 'xlc': 'communication services select sector'}

keys, values = zip(*spiders.items())
print ("keys : ", str(keys))

begin = "2020-05-01"
through = "2020-06-01"

yf.pdr_override()

collecting = True
savelist = []
j = 0

while collecting == True:
    #index = pdr.get_data_yahoo(keys[j], start=begin, end=through)
    #saver = index.to_excel(str(keys[j]) + '.xlsx')
    savename = str(keys[j]) + '.xlsx'
    savelist.append(savename)
    print(savelist)
    j = j + 1

    if j == len(values):
        collecting = False

df0 = pd.read_excel(savename[0], 'Sheet1')
df1 = pd.read_excel(savename[1], 'Sheet1')
df2 = pd.read_excel(savename[2], 'Sheet1')
df3 = pd.read_excel(savename[3], 'Sheet1')
df4 = pd.read_excel(savename[4], 'Sheet1')
df5 = pd.read_excel(savename[5], 'Sheet1')
df6 = pd.read_excel(savename[6], 'Sheet1')
df7 = pd.read_excel(savename[7], 'Sheet1')
df8 = pd.read_excel(savename[8], 'Sheet1')
df9 = pd.read_excel(savename[9], 'Sheet1')
df10 = pd.read_excel(savename[10], 'Sheet1')
df11 = pd.read_excel(savename[11], 'Sheet1')

date = df0['Date']
snp = df0['Adj Close']
xlp = df1['Adj Close']
xly = df2['Adj Close']
xlk = df3['Adj Close']
xlv = df4['Adj Close']
xli = df5['Adj Close']
xlu = df6['Adj Close']
xlb = df7['Adj Close']
xle = df8['Adj Close']
xlf = df9['Adj Close']
xlre = df10['Adj Close']
xlc = df11['Adj Close']'''

共1个答案

匿名用户

我认为您需要为每个read_excel语句使用累加器变量savelist,而不是循环中使用的变量savename。如前所述,这将获取savename最后一个值的每个字符,并将其解释为文件名。