我正在尝试迭代指定 path.How 中的 xlsx 文件以排除指定文件
I am trying to iterate a xlsx files in specified path.How to exclude specified file
我有 3 个 xlsx sheet 特别是 directory.I 正在组合成 workbook.While 组合,我需要排除指定的文件。
Path="C:/JackDaniels/100Pipers/"
name="Panic"
writer=ExcelWriter(Path+name+"*.xlsx")#creating a workbook in name "name")
inp=glob.glob(Path+"*.xlsx")
inp=inp.remove(Path+name+"*.xlsx")#to remove ths file to avoid overwrite
# I have a code that will combine sheets
当我尝试 运行 上面的代码时出现以下错误
list.remove(x):x not in list
问题确实不清楚,你应该改一下。
如果你试图将它们组合起来,你想将所有三个 sheets 附加到一个新的空 sheet (例如你所有的 sheets 都有相同的列)你应该在您的 excel 作品sheets:
所在目录中创建一个 python 文件
import copy
import os
import pandas as pd
cwd = os.getcwd()
# list to store files
xlsx_files = []
exc_file = 'Exclude.xlsx' # <-- The file name you want to exclude goes here.
out_file = 'Output.xlsx' # <-- The output file name goes here.
# Iterate directory
for file in os.listdir(cwd):
# check only Excel files
if file.endswith('.xlsx'):
xlsx_files.append(file)
print("All xlsx files:", xlsx_files)
df = pd.DataFrame()
aux_files_var = copy.deepcopy(xlsx_files)
for file in aux_files_var:
print(file)
if file == exc_file or file == out_file: continue # <-- here you exclude the file and the output
df = df.append(pd.read_excel(file), ignore_index=True)
xlsx_files.remove(file)
print(f"""As you can see, only exc_file remains in xlsx_files.
Remaining xlsx files:{xlsx_files}""")
print(df)
df.to_excel(out_file)
我有 3 个 xlsx sheet 特别是 directory.I 正在组合成 workbook.While 组合,我需要排除指定的文件。
Path="C:/JackDaniels/100Pipers/"
name="Panic"
writer=ExcelWriter(Path+name+"*.xlsx")#creating a workbook in name "name")
inp=glob.glob(Path+"*.xlsx")
inp=inp.remove(Path+name+"*.xlsx")#to remove ths file to avoid overwrite
# I have a code that will combine sheets
当我尝试 运行 上面的代码时出现以下错误
list.remove(x):x not in list
问题确实不清楚,你应该改一下。 如果你试图将它们组合起来,你想将所有三个 sheets 附加到一个新的空 sheet (例如你所有的 sheets 都有相同的列)你应该在您的 excel 作品sheets:
所在目录中创建一个 python 文件import copy
import os
import pandas as pd
cwd = os.getcwd()
# list to store files
xlsx_files = []
exc_file = 'Exclude.xlsx' # <-- The file name you want to exclude goes here.
out_file = 'Output.xlsx' # <-- The output file name goes here.
# Iterate directory
for file in os.listdir(cwd):
# check only Excel files
if file.endswith('.xlsx'):
xlsx_files.append(file)
print("All xlsx files:", xlsx_files)
df = pd.DataFrame()
aux_files_var = copy.deepcopy(xlsx_files)
for file in aux_files_var:
print(file)
if file == exc_file or file == out_file: continue # <-- here you exclude the file and the output
df = df.append(pd.read_excel(file), ignore_index=True)
xlsx_files.remove(file)
print(f"""As you can see, only exc_file remains in xlsx_files.
Remaining xlsx files:{xlsx_files}""")
print(df)
df.to_excel(out_file)