简体   繁体   English

使用 python 按列值移动多个 csv 文件

[英]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.

相关问题 使用python(pandas)从多个csv文件中过滤列值 - Filter column value from multiple csv files using python(pandas) 使用Python对多个csv文件中的每一列取平均值 - Take average of each column in multiple csv files using Python 如何使用pandas PYTHON按列中的值合并两个CSV文件 - How to merge two CSV files by value in column using pandas PYTHON 在PYTHON中,如何使用按值进行多列索引访问CSV单元? - In PYTHON, how to access a CSV cell using multiple column indexing by value? 分组以计算多个 CSV 文件中唯一 ID/itemName 的出现次数,并使用 pandas/python 对另一列中的值求和 - Group by to count occurrences of unique ID/itemName across multiple CSV files and sum up value in another column using pandas/python 根据列值将CSV拆分成多个文件 - Split CSV into multiple files based on column value 如何使用 python 使用列作为索引将多个 csv 文件连接到单个 csv 文件中 - How to concatenate multiple csv files into a single csv file using a column as index using python python csv文件中列值的精确匹配 - python exact match of column value in csv files 使用python在CSV文件中拆分单列 - Split single column in csv files using python Python:如何获取多个.csv文件的列的第一个值及其名称,并使用它们创建一个新文件 - Python: How to take the first value of a column of multiple .csv files + its name and make a new file with them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM