在 Python 脚本的帮助下重命名文件夹中的文件名,使用来自 Excel sheet 的名称映射
Rename the file names in the folder with the help of Python Script, using name map from Excel sheet
文件夹中有很多CSV文件,我想重命名。有一个 excel sheet 包含要重命名为文件夹的文件的名称。
文件夹中的文件命名为
TestData_30April.csv
TestData_20April.csv
TestData_18April.csv etc
而 excel sheet 包含名称为
0.25-TestData_30April
0.98-TestData_20April
0.33-TestData_20April etc
excel sheet 中的第一行也包含 Header 名称,而 wards 的第 2 行包含要重命名的文件名。
我的目标是重命名
TestData_30April.csv 到 0.25-TestData_30April.csv 同样适用于所有其他文件。
代码如下:
#Excel Sheet containing name of files to be renamed in that folder
path="C:\Users\Desktop\Test_Data\Test_Summary.csv"
#Folder Containg all orginal file names
dir = "C:\Users\Desktop\Wear_Data"
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
#In excel sheet column X or col_values(23) contains the file name to be renamed
print(sheet.col_values(23))
list_of_filename_in_folder = [] # name of the files in the folder
list_of_filename_in_excel = [] #name of the files in excel
path_to_folder = '' # base path of folder
for name in list_of_filename_in_excel:
excel_file_name = os.path.join(path_to_folder, name,'.csv')
newname = name
if '-' in name:
newname = name.split('-')[1]
dir_file_name = os.path.join(path_to_folder,newname,'.csv' )
if os.path.exists(dir_file_name):
print('changing file name {} to {}'.format(newname,name))
os.rename(dir_file_name, excel_file_name)
else:
print('no file {} with name found in location'.format(newname+'.csv')
这是错误:
XLRDError: Unsupported format, or corrupt file: Expected BOF record;
请帮助解决此错误。
虽然您可以打开带有 Excel 的 csv 文件,但 .csv
文件与通常的 excel 文件(以 .xlsx
结尾)不同。 Python 附带了一种非常方便的处理 csv 文件的方法: csv
module.
假设您的数据与示例中的一样,您可以执行以下操作:
import csv
import os
path= 'C:\Users\Desktop\Test_Data\Test_Summary.csv'
dir = 'C:\Users\Desktop\Wear_Data'
# open the .csv file with the csv module
with open(path, 'r') as f:
csv_file = csv.reader(f)
# read the new file name from every row
for row in csv_file:
# assuming the new file path is stored in the first column (= row[0])
new_file_name = row[0] + '.csv'
# your old file should always have the same pattern according to your example
old_file_name = new_file_name.split('-')[1] + '.csv'
old_file = os.path.join(dir, old_file_name)
new_file = os.path.join(dir, new_file_name)
# rename the file
os.rename(old_file, new_file)
我没有测试这个片段,但我认为它展示了使用 csv
模块如何工作的基本原理。
文件夹中有很多CSV文件,我想重命名。有一个 excel sheet 包含要重命名为文件夹的文件的名称。
文件夹中的文件命名为
TestData_30April.csv
TestData_20April.csv
TestData_18April.csv etc
而 excel sheet 包含名称为
0.25-TestData_30April
0.98-TestData_20April
0.33-TestData_20April etc
excel sheet 中的第一行也包含 Header 名称,而 wards 的第 2 行包含要重命名的文件名。
我的目标是重命名 TestData_30April.csv 到 0.25-TestData_30April.csv 同样适用于所有其他文件。
代码如下:
#Excel Sheet containing name of files to be renamed in that folder
path="C:\Users\Desktop\Test_Data\Test_Summary.csv"
#Folder Containg all orginal file names
dir = "C:\Users\Desktop\Wear_Data"
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
#In excel sheet column X or col_values(23) contains the file name to be renamed
print(sheet.col_values(23))
list_of_filename_in_folder = [] # name of the files in the folder
list_of_filename_in_excel = [] #name of the files in excel
path_to_folder = '' # base path of folder
for name in list_of_filename_in_excel:
excel_file_name = os.path.join(path_to_folder, name,'.csv')
newname = name
if '-' in name:
newname = name.split('-')[1]
dir_file_name = os.path.join(path_to_folder,newname,'.csv' )
if os.path.exists(dir_file_name):
print('changing file name {} to {}'.format(newname,name))
os.rename(dir_file_name, excel_file_name)
else:
print('no file {} with name found in location'.format(newname+'.csv')
这是错误:
XLRDError: Unsupported format, or corrupt file: Expected BOF record;
请帮助解决此错误。
虽然您可以打开带有 Excel 的 csv 文件,但 .csv
文件与通常的 excel 文件(以 .xlsx
结尾)不同。 Python 附带了一种非常方便的处理 csv 文件的方法: csv
module.
假设您的数据与示例中的一样,您可以执行以下操作:
import csv
import os
path= 'C:\Users\Desktop\Test_Data\Test_Summary.csv'
dir = 'C:\Users\Desktop\Wear_Data'
# open the .csv file with the csv module
with open(path, 'r') as f:
csv_file = csv.reader(f)
# read the new file name from every row
for row in csv_file:
# assuming the new file path is stored in the first column (= row[0])
new_file_name = row[0] + '.csv'
# your old file should always have the same pattern according to your example
old_file_name = new_file_name.split('-')[1] + '.csv'
old_file = os.path.join(dir, old_file_name)
new_file = os.path.join(dir, new_file_name)
# rename the file
os.rename(old_file, new_file)
我没有测试这个片段,但我认为它展示了使用 csv
模块如何工作的基本原理。