python - 根据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 文件
仍然无法获得所需的输出。
任何帮助将不胜感激。
谢谢!
解决方案
您可以遍历它们,提取日期以创建新文件名,然后保存。
# 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)
推荐阅读
- prestashop - Prestashop 1.7.6:在文本自定义区域中获取并显示 2° 产品图像作为背景
- angular - 切换标签时添加动画
- ffmpeg - ffmpeg 命令生成黑色图像作为输出
- nuxt.js - 如何配置 nuxt 或网页以使路由在末尾不带“/”
- node.js - Express 中间件配置响应
- angular - 使用 aot compile 时,angular 9 无法绑定
- flyway - Flyway 企业版是否允许 SqlServer 2012 数据库迁移自动化流程
- node.js - TypeError:this.findOne 不是函数
- html - Bootstrap4 布局问题 - 用内容填充浏览器页面高度
- c# - 如何将 Blazor 服务器端与 Blazor WebAssembly 结合使用?