ディレクトリからすべてのテキストファイルを読み込んで、それをExcelファイルに書き込むプログラムがあります。読み込み/保存されるファイルの順序を変更する方法
GetData_0.2017-12-04_160809
GetData_1.2017-12-04_160824
GetData_2.2017-12-04_160843
:すべてのテキストファイルは、名前のGetData _#+例えば、タイムスタンプ、あります私が持っているコードは次のとおりです。
:それは素晴らしい作品import os
import pandas as pd
import config
# Path of directory containing the text files
directory = config.Local_Recieved
# Initialize empty dataframe collector
dframe_collector = []
# For each file in the directory ...
for file_name in sorted(os.listdir(directory)):
if file_name.startswith('GetData'):
# Construct full path of file
file_path = os.path.join(directory, file_name)
# Read out file and store into a pandas dataframe
file_dframe = pd.read_csv(file_path, sep=';', header=None)
dframe_collector.append(file_dframe)
# Concatenate individual dataframes into one single dataframe
master_dframe = pd.concat(dframe_collector)
# With newly created excel file ...
with pd.ExcelWriter('DataLog.xlsx') as writer:
# For each unique parameter that occurs in the first column of the dataframe ...
for num, (name, group) in enumerate(master_dframe.groupby(0)):
# Write corresponding data rows to individual excel sheet
sheet_name = f"Sheet_{num}"
group.to_excel(writer, sheet_name=sheet_name, header=None, index=None)
しかし、私はこの部分のために問題を抱えています
# For each file in the directory ...
for file_name in sorted(os.listdir(directory)):
if file_name.startswith('GetData'):
# Construct full path of file
file_path = os.path.join(directory, file_name)
# Read out file and store into a pandas dataframe
file_dframe = pd.read_csv(file_path, sep=';', header=None)
dframe_collector.append(file_dframe)
ファイルを読み取る順序は、次のとおりです。GetData_0,1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24、.. 。 ご希望の注文はGetData_0,1,2,3,4,5,6,7,8,9,10、...、501,502
変更する必要がありますか?