[英]move multiple csv files by column value using python
I have thousands of csv files under uncategorised parent folder, all the files have one date column containing same date for all the rows.我在未分类的父文件夹下有数千个 csv 文件,所有文件都有一个日期列,其中包含所有行的相同日期。 I want to check the date value of each files and move/copy to month wise folder using python.我想检查每个文件的日期值并使用 python 移动/复制到月明智的文件夹。
I have tried key = df.iloc[0]['Date'] but not able to use key.endswith or key.我试过 key = df.iloc[0]['Date'] 但无法使用 key.endswith 或 key。 contains包含
Here I am looping through the files, reading the first row of the date column.在这里,我循环浏览文件,读取日期列的第一行。 I have created new folders in the directory previously, with the names being each month of the year.我之前在目录中创建了新文件夹,名称是一年中的每个月。 Once I have read the date, I convert it to words (eg April, May).读取日期后,我将其转换为文字(例如四月、五月)。 I then look through folders in the directory and is the date name and folder name match, I move the file to the folder.然后我查看目录中的文件夹并且日期名称和文件夹名称匹配,我将文件移动到文件夹中。
import os
import pandas as pd
import datetime
files = os.listdir()
for file in files:
if ".csv" in file:
df = pd.read_csv(file)
dates = df['date']
date = dates[0]
date = datetime.datetime.strptime(date, "%d/%m/%y")
date = date.strftime("%B")
for folder in files:
if date.lower() == folder.lower():
os.rename(file, folder+"\\"+file)
If all CSV files have the same structure, for example:如果所有CSV个文件结构相同,例如:
date, col_1, col_2, ..., col_n
20210923, 123, 456, ..., 999
You can try:你可以试试:
from os import mkdir
from os.path import join, exists, isdir
from glob import glob
from shutil import move
from datetime import datetime
import csv
FOLDER_CSV = 'data/csv'
FOLDER_MONTHS = 'data/csv_by_month'
DATE_FORMAT = '%Y%m%d'
if not exists(FOLDER_MONTHS):
mkdir(FOLDER_MONTHS)
for file in glob(join(FOLDER_CSV, '*.csv')):
with open(file, newline='') as ftx:
reader = csv.reader(ftx)
row = reader.__next__()
row = reader.__next__()
# Replaces the row[0] Index for index in the date column in the CSV
month = datetime.strptime(row[0], DATE_FORMAT).strftime('%B').lower()
if not exists(join(FOLDER_MONTHS, month)):
mkdir(join(FOLDER_MONTHS, month))
try:
if isdir(join(FOLDER_MONTHS, month)):
move(file, join(FOLDER_MONTHS, month))
except OSError:
print(f'{file} file is already in the {month} directory.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.