python读取txt文件排班
时间: 2023-10-31 09:07:40 浏览: 171
Python读取txt文件排班的过程可以分为以下几个步骤:
1. 在同文件夹中建立排班工时对应表.txt的文件,每一行写入排班类别和对应工时,按约定的分隔符分隔。
2. 读取txt文件,将其转换为字典格式。
3. 读取Excel文件,使用Pandas库进行数据处理。
4. 将数据写入Excel文件中。
具体实现过程可以参考以下代码:
# 读取txt文件,将其转换为字典格式
trans_dict = pd.read_table('./排班工时对应表.txt', sep=' ', index_col=0)
str2time = trans_dict['工时'].to_dict()
# 读取Excel文件,使用Pandas库进行数据处理
data = pd.read_excel('./排班.xlsx', index_col=3, skiprows=1)
data = data.iloc[:, 3:10]
data.dropna(how='all', inplace=True)
data.replace(str2time, inplace=True)
# 将数据写入Excel文件中
book = load_workbook('./排班.xlsx')
write = pd.ExcelWriter(r'./排班.xlsx', engine='openpyxl')
write.book = book
write.sheets = {ws.title: ws for ws in book.worksheets}
man_hour.to_excel(write, sheet_name='sheet1', index=False, header=False, startcol=11, startrow=2)
write.close()
阅读全文
相关推荐














