160 lines
6.0 KiB
Python
160 lines
6.0 KiB
Python
import os
|
|
import logging
|
|
import chardet # pip install chardet
|
|
|
|
# 替换为你的目录路径
|
|
directory_path = r'D:\Dp10RepoV1\项目代码\D10myWeiao' # T01贸易生产计划 T02贸易汇总仓库
|
|
# 设置日志记录
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
# 定义替换列表
|
|
replacements = [
|
|
('dxSkinsCore,', ''),
|
|
('dxSkinsDefaultPainters,', ''),
|
|
('dxSkinWXI,', ''),
|
|
('dxSkinBasic,', ''),
|
|
('dxSkinBlack,', ''),
|
|
('dxSkinBlue,', ''),
|
|
('dxSkinBlueprint,', ''),
|
|
('dxSkinCaramel,', ''),
|
|
('dxSkinCoffee,', ''),
|
|
('dxSkinDarkroom,', ''),
|
|
('dxSkinDarkSide,', ''),
|
|
('dxSkinDevExpressDarkStyle,', ''),
|
|
('dxSkinDevExpressStyle,', ''),
|
|
('dxSkinFoggy,', ''),
|
|
('dxSkinGlassOceans,', ''),
|
|
('dxSkinHighContrast,', ''),
|
|
('dxSkiniMaginary,', ''),
|
|
('dxSkinLilian,', ''),
|
|
('dxSkinLiquidSky,', ''),
|
|
('dxSkinLondonLiquidSky,', ''),
|
|
('dxSkinMcSkin,', ''),
|
|
('dxSkinMetropolis,', ''),
|
|
('dxSkinMetropolisDark,', ''),
|
|
('dxSkinMoneyTwins,', ''),
|
|
('dxSkinOffice2007Black,', ''),
|
|
('dxSkinOffice2007Blue,', ''),
|
|
('dxSkinOffice2007Green,', ''),
|
|
('dxSkinOffice2007Pink,', ''),
|
|
('dxSkinOffice2007Silver,', ''),
|
|
('dxSkinOffice2010Black,', ''),
|
|
('dxSkinOffice2010Blue,', ''),
|
|
('dxSkinOffice2010Silver,', ''),
|
|
('dxSkinOffice2013DarkGray,', ''),
|
|
('dxSkinOffice2013LightGray,', ''),
|
|
('dxSkinOffice2013White,', ''),
|
|
('dxSkinOffice2016Colorful,', ''),
|
|
('dxSkinOffice2016Dark,', ''),
|
|
('dxSkinOffice2019Black,', ''),
|
|
('dxSkinOffice2019Colorful,', ''),
|
|
('dxSkinOffice2019DarkGray,', ''),
|
|
('dxSkinOffice2019White,', ''),
|
|
('dxSkinPumpkin,', ''),
|
|
('dxSkinSeven,', ''),
|
|
('dxSkinSevenClassic,', ''),
|
|
('dxSkinSharp,', ''),
|
|
('dxSkinSharpPlus,', ''),
|
|
('dxSkinSilver,', ''),
|
|
('dxSkinSpringtime,', ''),
|
|
('dxSkinStardust,', ''),
|
|
('dxSkinSummer2008,', ''),
|
|
('dxSkinTheAsphaltWorld,', ''),
|
|
('dxSkinTheBezier,', ''),
|
|
('dxSkinValentine,', ''),
|
|
('dxSkinVisualStudio2013Blue,', ''),
|
|
('dxSkinVisualStudio2013Dark,', ''),
|
|
('dxSkinVisualStudio2013Light,', ''),
|
|
('dxSkinVS2010,', ''),
|
|
('dxSkinWhiteprint,', ''),
|
|
('dxSkinXmas2008Blue,', ''),
|
|
('dxSkinXmas2008Blue;', ';')
|
|
]
|
|
|
|
|
|
def replace_in_file(file_path, replacements):
|
|
temp_file_path = file_path + '.tmp'
|
|
try:
|
|
# 使用chardet检测文件编码
|
|
with open(file_path, 'rb') as infile:
|
|
raw_data = infile.read()
|
|
detected_encoding = chardet.detect(raw_data)['encoding']
|
|
with open(file_path, 'r', encoding=detected_encoding) as infile, \
|
|
open(temp_file_path, 'w', encoding=detected_encoding) as outfile:
|
|
# 读取整个文件内容到一个字符串中
|
|
file_content = infile.read()
|
|
# 对文件内容进行所有替换操作
|
|
for old, new in replacements:
|
|
file_content = file_content.replace(old, new)
|
|
# 将替换后的内容写回临时文件
|
|
outfile.write(file_content)
|
|
# 使用os.replace原子性地替换原文件
|
|
os.replace(temp_file_path, file_path)
|
|
logging.info(f"Successfully processed file: {file_path} using encoding {detected_encoding}")
|
|
except Exception as e:
|
|
logging.error(f"Error processing file {file_path}: {e}")
|
|
if os.path.exists(temp_file_path):
|
|
os.remove(temp_file_path)
|
|
|
|
|
|
# =========================================批处理上述运行后出现逗号紧挨着分号的情况==============================
|
|
|
|
def remove_empty_lines_in_first_50_lines(filename):
|
|
with open(filename, 'rb') as file:
|
|
raw_data = file.read()
|
|
detected_encoding = chardet.detect(raw_data)['encoding']
|
|
with open(filename, 'r', encoding=detected_encoding) as file:
|
|
lines = file.readlines()
|
|
non_empty_lines = [line for line in lines[:50] if line.strip() != '']
|
|
with open(filename, 'w', encoding=detected_encoding) as file:
|
|
file.writelines(non_empty_lines + lines[50:])
|
|
|
|
|
|
def remove_single_semicolon_lines(filename):
|
|
with open(filename, 'rb') as file:
|
|
raw_data = file.read()
|
|
detected_encoding = chardet.detect(raw_data)['encoding']
|
|
with open(filename, 'r', encoding=detected_encoding) as file:
|
|
lines = file.readlines()
|
|
non_single_semicolon_lines = [line for line in lines if line.strip() != ';' and line.strip() != '']
|
|
with open(filename, 'w', encoding=detected_encoding) as file:
|
|
file.writelines(non_single_semicolon_lines)
|
|
|
|
|
|
def modify_last_comma_line(filename):
|
|
with open(filename, 'rb') as file:
|
|
raw_data = file.read()
|
|
detected_encoding = chardet.detect(raw_data)['encoding']
|
|
with open(filename, 'r', encoding=detected_encoding) as file:
|
|
lines = file.readlines()
|
|
if len(lines) > 50:
|
|
first_50_lines = lines[:50]
|
|
# 找到仅包含 "type" 的行
|
|
type_lines = [line for line in first_50_lines if "type" in line]
|
|
if type_lines:
|
|
# 获取 "type" 行的上一行
|
|
last_type_line_index = first_50_lines.index(type_lines[-1]) - 1
|
|
last_type_line = first_50_lines[last_type_line_index]
|
|
if last_type_line.strip().endswith(','):
|
|
# 找到最后一个逗号的位置
|
|
last_comma_index = last_type_line.rfind(',')
|
|
# 将最后一个逗号替换为分号
|
|
lines[lines.index(last_type_line)] = last_type_line[:last_comma_index] + ';\n'
|
|
with open(filename, 'w', encoding=detected_encoding) as file:
|
|
file.writelines(lines)
|
|
|
|
|
|
def replace_in_folder(folder_path, replacements):
|
|
for root, dirs, files in os.walk(folder_path):
|
|
for file_name in files:
|
|
if file_name.endswith('.pas'):
|
|
file_path = os.path.join(root, file_name)
|
|
replace_in_file(file_path, replacements)
|
|
remove_empty_lines_in_first_50_lines(file_path)
|
|
remove_single_semicolon_lines(file_path)
|
|
modify_last_comma_line(file_path)
|
|
|
|
|
|
# 执行替换操作
|
|
replace_in_folder(directory_path, replacements)
|