简体   繁体   English

我需要从多个 csv 文件名中捕获日期,并使用 Python 在每个文件中将该日期添加为新列

[英]I need to capture date from multiple csv filenames and add that date in each file as a new column using Python

I need to capture date from multiple csv filenames and add that date in each file as a new column using Python, I have this code that works well with Excel files and I am trying to do exactly the same with CSV files, If someone could help me that would be much appreciated. I need to capture date from multiple csv filenames and add that date in each file as a new column using Python, I have this code that works well with Excel files and I am trying to do exactly the same with CSV files, If someone could help我将不胜感激。

Filenames are as following... Scan_05-22-2021.csv Scan_05-23-2021.csv Scan_05-24-2021.csv and so on..文件名如下... Scan_05-22-2021.csv Scan_05-23-2021.csv Scan_05-24-2021.Z628CB5675FF524F3EZFEE88 等。

Excel code that works.. Excel 代码有效..

import openpyexcel
import os
import pandas as pd
import glob
import csv

from openpyexcel import load_workbook
import os

path_to_xls = os.getcwd() # or r'<path>'

for xls in os.listdir ('C:\Python'):
    if xls.endswith(".csv") or xls.endswith(".xlsx"):
        f = load_workbook(filename=xls)
        sheet = f.active
        # Change here the name of the new column
        sheet.cell(row=1, column=25).value = "DateTest"
        for i in range(sheet.max_row-1):
            #xls.split('_')[1][:-5]    #kaes value of Col1 and dumps/overwrites in column 3
            sheet.cell(row=i+2, column=25).value = xls.split('_')[1][:-5]

                f.save(xls)
                f.close()

You should be able to do this with pandas你应该可以用 pandas 做到这一点

use pd.read_csv to load the files as DataFrames you can use the iterrows method to go ever rows and simply append to the new file.使用 pd.read_csv 将文件加载为 DataFrames,您可以使用 iterrows 方法将 go 任何行和简单的 append 用于新文件。

this cheatsheet could be of use这个备忘单可能有用

Good luck!祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历包含 csv 文件的文件夹,提取每个文件的修改日期并使用 Python 创建一个新列作为“修改日期” - How to iterate through a folder that contains csv files, extract each file's modified date and create a new column as " modified date" using Python 如何使用 pandas 将基于产品保质期从现有日期列派生的新日期列添加到我的 csv 文件中? - How to add a new date column derived from existing date column based on the shelf life of the products to my csv file using pandas? 使用Python在csv文件中添加新列? - Add a new column in a csv file using Python? 使用python将新列添加到CSV文件 - Add a new column to a CSV file using python Python 从多个 CSV 文件中读取数据并将每个文件添加到新列 - Python Reading data from multiple CSV files and adding each file to a new column 如何通过使用Python对CSV文件中的日期进行排序来获取列中的最大值? - How can I get the highest value in a column by sorting the date in a CSV file using Python? 如何使用 python 中的 pandas 转换 csv 文件中的列中的日期格式? - How do I convert date format in a column in a csv file using pandas in python? 如何使用python从csv中的同一列拆分日期和时间? - how to split date and time from same column in csv using python? 如何使用python从JSON添加csv中的日期时间值? - How to add date time values in csv from json using python? 如何使用 Pandas 从 csv 文件的列中提取日期? - How do I extract the date from a column in a csv file using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM