python-读取EXCEL多个sheet数据写入csv(四)
需求 读取一个EXCEL所有的sheet数据 多次运行追加写入csv文件
# -*- coding: utf-8 -*- """ Created on 2021/12/29 19:20 @author: tangge """ import pandas as pd import time path = "D:/code_fileAll/test_file/input/老头乐.xlsx" res_path = "D:/code_fileAll/test_file/output/" curr_date = time.strftime("%Y%m%d", time.localtime()) print(curr_date) # 读取EXCEL,设定列名,None读取所有sheet, 默认只读第一个 df = pd.read_excel(path, names=[1, 2, 3, 4], sheet_name=None) a = [] # items 拆分数据为元组 for name , data in df.items(): a.append(data) # axis=0 按列上下拼接,1为行左右拼接 df2 = pd.concat(a, axis=0)a df=df2 # 拼接文件路径与文件名 res_file_name = f"{ res_path}xlsxToCsv_{ curr_date}.csv" # 第4列值包含两个中文的字符,只要两列, 与下面的to_csv中column功能重复 # case 大小写设置 , na 空值设置(fillna 在此处无效) df_2 = df[df[4].str.contains("^[一-龥]{2}$", case=True , na=False)].loc[:, [1, 2]] # 追加写入,不要索引,不要表头, 选取固定列 mode=a 追加, mode=w-->覆盖, 只要两列 df_2.to_csv(res_file_name, mode=a, sep=,, index=None, header=None)
下一篇:
VC++检测和解决GDI泄漏问题