Python 在Pandas中读取多个文件
我似乎找不到解决这个问题的办法 我使用pandas读取我的第一个excel文件并提取信息。然后,我使用这些信息更新一个新的excel文件,该文件反过来更新公式 我想再次使用pandas来读取新文件,但它仍在读取原始文件。我使用Python 在Pandas中读取多个文件,python,excel,python-3.x,pandas,Python,Excel,Python 3.x,Pandas,我似乎找不到解决这个问题的办法 我使用pandas读取我的第一个excel文件并提取信息。然后,我使用这些信息更新一个新的excel文件,该文件反过来更新公式 我想再次使用pandas来读取新文件,但它仍在读取原始文件。我使用filename.close()关闭第一个文件,但它仍然记得该文件中的所有内容。它不会读取第二个文件 这是读取第一个文件 firstDate = [] dateHeader = [] dataList = []` xl = pandas.ExcelFile(xlsx) s
filename.close()
关闭第一个文件,但它仍然记得该文件中的所有内容。它不会读取第二个文件
这是读取第一个文件
firstDate = []
dateHeader = []
dataList = []`
xl = pandas.ExcelFile(xlsx)
sheets = xl.sheet_names
df = xl.parse(sheets[2])
for index, row in df.iterrows():
dateStamp = row[2].date()
creationMonth = dateStamp.month
creationDay = dateStamp.day
creationYear = dateStamp.year
creationDate = datetime.date(creationYear, creationMonth, 1)
endDate = datetime.datetime.now()
#Create a list to hold all the dates
dateList = []
#Iterate through the different dates
for i, dt in enumerate(rrule(MONTHLY, dtstart=creationDate, until=endDate)):
#Format the date into the correct format
date = dt.strftime("%m/%d/%Y")
#Create a condition to skip of the present date
if i > 0:
#Append the date to the dateList
dateList.append(date)
#Get the length of the date list
lenLst = len(dateList)
#Create the date range list
dateRangeList = []
#Iterate through the date list
for i, value in enumerate(dateList):
#Create a check to not fo beyond the bounds of the list
if i != (lenLst - 1):
#Get the start date
sRange = dateList[i]
#Get the end date
eRange = dateList[i + 1]
#Set the date range
dateRange = [sRange, eRange]
#Append the rnage to the date range list
dateRangeList.append(dateRange)
if dateStamp not in firstDate:
firstDate.append(dateStamp)
baseData = [row[0], '%s/%s/%s' %(creationMonth, creationDay, creationYear), row[3], dateRangeList]
dataList.append(baseData)
xl.close()
这将读取第二个文件
for i, value in enumerate(dataList):
wb = openpyxl.load_workbook(xlsx)
worksheets = wb.sheetnames
worksheet = wb.get_sheet_by_name(worksheets[0])
rowCoordinate = i
meterName = value[0]
creationDate = value[1]
units = value[2]
worksheet.cell(row=1, column=2).value = meterName
wb.save(copyXlsx)
dateList = []
for k, dateRange in enumerate(value[3]):
sDate = dateRange[0]
eDate = dateRange[1]
wb = openpyxl.load_workbook(copyXlsx)
worksheets = wb.sheetnames
worksheet = wb.get_sheet_by_name(worksheets[0])
worksheet.cell(row=2, column=2).value = sDate
worksheet.cell(row=3, column=2).value = eDate
wb.save(copyXlsx1)
print meterName, dateRange
xl1 = pandas.ExcelFile(copyXlsx1)
sheets1 = xl1.sheet_names
df = xl1.parse(sheets1[0])
print df
你能把这封信寄出去吗code@NickHale我已经发布了代码,但是pandas.ExcelFile没有
上下文管理器
?如果你有两个excel文件,为什么要给它们取相同的名字?excel文件有不同的名字。第一个变量名为xlsx,第二个变量名为xlsxCopy还有一个xlsxcopy1那么的意思是什么?我想再次使用pandas读取新文件,但它仍在读取原始文件。我使用filename.close()关闭第一个文件,但它仍然记得该文件中的所有内容。它不会读取第二个文件。
您可以发布code@NickHale我已经发布了代码,但是pandas.ExcelFile没有上下文管理器
?如果你有两个excel文件,为什么要给它们取相同的名字?excel文件有不同的名字。第一个变量名为xlsx,第二个变量名为xlsxCopy还有一个xlsxcopy1那么的意思是什么?我想再次使用pandas读取新文件,但它仍在读取原始文件。我使用filename.close()关闭第一个文件,但它仍然记得该文件中的所有内容。它不会读取第二个文件。