应该是这个问题,我刚刚也自己写了一下,这样可以。
[Python] 纯文本查看 复制代码 #!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import numpy as np
import pandas as pd
from openpyxl import Workbook
# 声明源数据路径
pathSourceData = "源数据"
# 声明新派、在途和归档3个文件列表
listFilesNewDistribution = []
listFilesOnTheWay = []
listFilesClosedLoop = []
# 获得源数据文件夹的全部文件名称,并对CSV文件进行分类
dirSourceData = os.listdir(pathSourceData)
for file in dirSourceData:
if os.path.splitext(file)[1] == ".csv":
if "新派" in file:
listFilesNewDistribution.append(file)
elif "在途" in file:
listFilesOnTheWay.append(file)
elif "归档" in file:
listFilesClosedLoop.append(file)
print(listFilesNewDistribution)
print(listFilesOnTheWay)
print(listFilesClosedLoop)
# ###循环输入新派文件数据###
# 如果有新派数据,就先输入第一个,再循环输入剩余的并合并
if len(listFilesNewDistribution) < 1:
print("[错误] 未找到新派源数据")
else:
# 先输入第1个数据
sheetNewDistribution = pd.read_csv(pathSourceData + "\\" + listFilesNewDistribution[0], encoding='gbk')
# 循环输入剩余的数据并合并
for i in range(len(listFilesNewDistribution) - 1):
i += 1
sheetTemp = pd.read_csv(pathSourceData + "\\" + listFilesNewDistribution[i], encoding='gbk')
sheetNewDistribution = pd.concat([sheetNewDistribution, sheetTemp])
print(sheetNewDistribution)
print("[操作] 输出新派EXCEL表")
sheetNewDistribution.to_excel('xlsxNewDistribution.xlsx', sheet_name='mySheet', encoding="utf_8_sig", index=False) |