Skip to content

Instantly share code, notes, and snippets.

@Hosuke
Last active August 5, 2023 19:43
Show Gist options
  • Save Hosuke/c1214d9920cc549edaa535fe2fe41614 to your computer and use it in GitHub Desktop.
Save Hosuke/c1214d9920cc549edaa535fe2fe41614 to your computer and use it in GitHub Desktop.
import os
import re
# 获取当前工作目录
directory = os.getcwd()
# 定义日期的正则表达式
date_pattern = r"'((19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01]))'"
# 遍历目录及其所有子目录下的所有文件
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
# 只处理.sql文件
if filename.endswith('.sql'):
filepath = os.path.join(dirpath, filename)
# 读取文件内容
with open(filepath, 'r') as file:
filedata = file.read()
# 使用正则表达式替换日期,忽略大小写
filedata = re.sub(date_pattern, r"TIMESTAMP '\1'", filedata, flags=re.IGNORECASE)
# 将修改后的内容写回文件
with open(filepath, 'w') as file:
file.write(filedata)
print("Date replacement completed!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment