【发布时间】:2017-12-12 15:03:06
【问题描述】:
我有一个程序可以从目录中读取所有文本文件并将其放入 excel 文件中。所有文本文件的名称为GetData_#+timestamp,例如:
GetData_0.2017-12-04_160809
GetData_1.2017-12-04_160824
GetData_2.2017-12-04_160843
我的代码是:
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
我需要改变什么?
【问题讨论】:
标签: python python-3.x