首页 > 解决方案 > 根据python中csv文件中的一些内容重命名csv文件

问题描述

我在一个子文件夹中有许多 csv 文件,比如数据。这些 .csv 文件中的每一个都包含一个日期列。

430001.csv, 43001(1).csv,43001(2).csv,..........,43001(110).csv etc.

我想根据csv文件列内的日期重命名文件夹中的所有文件。

期望的输出:

430001-1980.csv, 43001-1981.csv,43001-1985.csv,..........,43001-2010.csv etc.

我尝试按照以下建议的步骤操作: 重命名多个 csv 文件

仍然无法获得所需的输出。

任何帮助将不胜感激。

谢谢!

标签: pythonpandascsvtime-series

解决方案


您可以遍历它们,提取日期以创建新文件名,然后保存。

# packages to import
import os
import pandas as pd
import glob
import sys

data_p = "Directory with your data"
output_p = "Directory where you want to save your output"
retval = os.getcwd() 
print (retval) # see in which folder you are

os.chdir(data_p) # move to the folder with your data
os.getcwd()

filenames = sorted(glob.glob('*.csv'))
fnames = list(filenames) # get the names of all your files
#print(fnames) 

for f in range(len(fnames)):
    print(f'fname: {fnames[f]}\n')
    pfile = pd.read_csv(fnames[f], delimiter=",") # read in file

    #extract filename
    filename = fnames[f]
    parts = filename.split(".") # giving you the number in file name and .csv
    only_id = parts[0].split("(") # if there is a bracket included 

    # get date from your file
    filedate = pfile["date"][0] # assuming this is on the first row
    filedate = str(filedate)

    # get new filename
    newfilename = only_id[0]+"-"+filedate+parts[1]

    # save your file (don't put a slash at the end of your directories on top)
    pfile.to_csv(output_p+"/"+newfilename, index = False, header = True)
    
   


推荐阅读