I'm trying to understand a code that was published a while ago. I couldn't find a proper explanation so I made this post. I'm new to python and If someone could explain me how it works I would really appreciate that. I have marked parts which I don't understand.
Code itself:
from pandas import ExcelWriter
import glob
import os
import pandas as pd
writer = ExcelWriter("output.xlsx")
for filename in glob.glob("*.xlsx"):
excel_file = pd.ExcelFile(filename)
(_, f_name) = os.path.split(filename) <---
(f_short_name, _) = os.path.splitext(f_name)
for sheet_name in excel_file.sheet_names:
df_excel = pd.read_excel(filename, sheet_name=sheet_name)
df_excel.to_excel(writer, f_short_name+'_'+sheet_name, index=False) <---
writer.save()
What you mark is just the creator try to name the sheet in a new excel file.
(_, f_name) = os.path.split(filename)
# _: 'C:\\Desktop'
#f_name: 'file.xlsx'
It returns head and tail of the directory. In this case the tail is file name.
df_excel.to_excel(writer, f_short_name+'_'+sheet_name, index=False)
This one is to save a dataframe to excel file. First argument:
ExcelWriter
if you want to create an excel file with multiple sheet inside. file path
(Where do you want to save it). Second argument : Sheet name
for each dataframe.
last argument : index
or the row name
for further information:
os.path.split
: https://www.geeksforgeeks.org/python-os-path-split-method/
.to_excel
: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.