根据python中csv文件内部的一些内容重命名csv文件



我在一个子文件夹中有许多csv文件,比如data。每个.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文件

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

如有任何帮助,我们将不胜感激。

谢谢!

您可以循环浏览它们,提取日期以创建新的文件名,然后保存。

# 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)


最新更新