1 回答
TA贡献1854条经验 获得超8个赞
我将使用这个模拟文件结构(使用tree命令绘制,并保存~/test/在我的计算机中):
test
└── 408
└── 2010
└── 01
├── 21
│ ├── 1.csv
│ └── 2.csv
├── 22
│ ├── 1.csv
│ └── 2.csv
└── 23
├── 1.csv
└── 2.csv
您可以使用 Python 重命名文件,并使用以下命令pathlib将它们连接起来pandas:
import pandas as pd
from pathlib import Path
def getfolders(files):
return sorted(list(set([file.parent for file in files])))
def getpathproperty(folder, prop):
properties = {"orderno": 3, "year": 2, "month": 1, "day": 0}
for i in range(properties[prop]):
folder = folder.parent
return folder.stem
path = Path("~/test").expanduser()
allfiles = list(path.rglob("*.csv")) # Each file in allfiles is a Path object
folders = getfolders(allfiles)
for folder in folders:
files = sorted(list(folder.glob("*.csv")))
df = pd.concat([pd.read_csv(file) for file in files])
# Get the values from the path to rename the files
orderno = getpathproperty(folder, "orderno")
year = getpathproperty(folder, "year")
month = getpathproperty(folder, "month")
day = getpathproperty(folder, "day")
# Save the new CSV file
df.to_csv(folder/f"{orderno}_{year}_{month}_{day}.csv", index=False)
# Delete old files, commented for safety
# for file in files:
# file.unlink(missing_ok=True)
这产生:
test
└── 408
└── 2010
└── 01
├── 21
│ └── 408_2010_01_21.csv
├── 22
│ └── 408_2010_01_22.csv
└── 23
└── 408_2010_01_23.csv
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报