Last active
August 27, 2024 17:52
-
-
Save Cayahuanca/a016d6dea17f943230ab22c0a598be43 to your computer and use it in GitHub Desktop.
VRCX のデータベースを、マージするときに使用する。データベースから、テーブルごとに .sql ファイルを作り、それに対して行う。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 日付時刻と、id(int) の主キーがある場合のファイル | |
import re | |
import argparse | |
import os | |
def process_files(file1, file2): | |
# 拡張子を取得 | |
base1, ext1 = os.path.splitext(file1) | |
base2, ext2 = os.path.splitext(file2) | |
# 出力ファイル名 | |
output_file = 'sorted1.sql' | |
unmatched_file = 'sorted2.sql' | |
# ファイル読み込み (UTF-8 エンコーディング) | |
with open(file1, 'r', encoding='utf-8') as f1, open(file2, 'r', encoding='utf-8') as f2: | |
lines = f1.readlines() + f2.readlines() | |
# 正規表現パターン | |
date_pattern = r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z)' | |
number_pattern = r'\((\d+),' | |
# 一致する行と一致しない行を分ける | |
matched_lines = [] | |
unmatched_lines = [] | |
for line in lines: | |
if re.search(date_pattern, line): | |
matched_lines.append(line) | |
else: | |
unmatched_lines.append(line) | |
# 一致する行の処理 | |
unique_lines = list(set(matched_lines)) | |
sorted_lines = sorted(unique_lines, key=lambda x: re.search(date_pattern, x).group()) | |
# 最初の行の数値を取得し、最小の数値に設定 | |
first_number_match = re.search(number_pattern, sorted_lines[0]) | |
if first_number_match: | |
min_number = int(first_number_match.group(1)) | |
else: | |
min_number = 1 # 何かエラーがある場合に備えてデフォルト値を設定 | |
# 各行の数値部分を置き換え、数値を順次増加させる | |
renumbered_lines = [] | |
for i, line in enumerate(sorted_lines): | |
new_number = min_number + i | |
new_line = re.sub(number_pattern, f'({new_number},', line) | |
renumbered_lines.append(new_line) | |
# ソート結果を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(output_file, 'w', encoding='utf-8') as f: | |
f.writelines(renumbered_lines) | |
# 一致しない行を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(unmatched_file, 'w', encoding='utf-8') as f: | |
f.writelines(unmatched_lines) | |
print(f"処理結果が {output_file} に書き出されました。") | |
print(f"一致しなかった行が {unmatched_file} に書き出されました。") | |
# 入力ファイルの削除 | |
os.remove(file1) | |
os.remove(file2) | |
print(f"入力ファイル {file1} と {file2} が削除されました。") | |
if __name__ == "__main__": | |
# コマンドライン引数の処理 | |
parser = argparse.ArgumentParser(description="2つのファイルを結合し、一致する行を処理し、一致しない行を別ファイルに書き出します。") | |
parser.add_argument("file1", help="処理する最初のファイル名を指定してください。") | |
parser.add_argument("file2", help="処理する2つ目のファイル名を指定してください。") | |
args = parser.parse_args() | |
# ファイルを処理して書き出す関数を呼び出す | |
process_files(args.file1, args.file2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 日付時刻はあるが、id(int) の主キーがない場合のファイル | |
import re | |
import argparse | |
import os | |
def process_files(file1, file2): | |
# 拡張子を取得 | |
base1, ext1 = os.path.splitext(file1) | |
base2, ext2 = os.path.splitext(file2) | |
# 出力ファイル名 | |
output_file = 'sorted1.sql' | |
unmatched_file = 'sorted2.sql' | |
# ファイル読み込み (UTF-8 エンコーディング) | |
with open(file1, 'r', encoding='utf-8') as f1, open(file2, 'r', encoding='utf-8') as f2: | |
lines = f1.readlines() + f2.readlines() | |
# 正規表現パターン | |
date_pattern = r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z)' | |
# 一致する行と一致しない行を分ける | |
matched_lines = [] | |
unmatched_lines = [] | |
for line in lines: | |
if re.search(date_pattern, line): | |
matched_lines.append(line) | |
else: | |
unmatched_lines.append(line) | |
# 一致する行の処理 | |
unique_lines = list(set(matched_lines)) | |
sorted_lines = sorted(unique_lines, key=lambda x: re.search(date_pattern, x).group()) | |
# ソート結果を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(output_file, 'w', encoding='utf-8') as f: | |
f.writelines(sorted_lines) | |
# 一致しない行を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(unmatched_file, 'w', encoding='utf-8') as f: | |
f.writelines(unmatched_lines) | |
print(f"処理結果が {output_file} に書き出されました。") | |
print(f"一致しなかった行が {unmatched_file} に書き出されました。") | |
# 入力ファイルの削除 | |
os.remove(file1) | |
os.remove(file2) | |
print(f"入力ファイル {file1} と {file2} が削除されました。") | |
if __name__ == "__main__": | |
# コマンドライン引数の処理 | |
parser = argparse.ArgumentParser(description="2つのファイルを結合し、一致する行を処理し、一致しない行を別ファイルに書き出します。") | |
parser.add_argument("file1", help="処理する最初のファイル名を指定してください。") | |
parser.add_argument("file2", help="処理する2つ目のファイル名を指定してください。") | |
args = parser.parse_args() | |
# ファイルを処理して書き出す関数を呼び出す | |
process_files(args.file1, args.file2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 日付時刻も、id(int) の主キーもない場合のファイル | |
import re | |
import argparse | |
import os | |
def process_files(file1, file2): | |
# 拡張子を取得 | |
base1, ext1 = os.path.splitext(file1) | |
base2, ext2 = os.path.splitext(file2) | |
# 出力ファイル名 | |
output_file = 'sorted1.sql' | |
unmatched_file = 'sorted2.sql' | |
# ファイル読み込み (UTF-8 エンコーディング) | |
with open(file1, 'r', encoding='utf-8') as f1, open(file2, 'r', encoding='utf-8') as f2: | |
lines = f1.readlines() + f2.readlines() | |
# 正規表現パターン | |
pattern = "INSERT INTO" | |
# 一致する行と一致しない行を分ける | |
matched_lines = [] | |
unmatched_lines = [] | |
for line in lines: | |
if re.search(pattern, line): | |
matched_lines.append(line) | |
else: | |
unmatched_lines.append(line) | |
# 一致する行の処理 | |
unique_lines = list(set(matched_lines)) | |
sorted_lines = sorted(unique_lines, key=lambda x: re.search(pattern, x).group()) | |
# ソート結果を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(output_file, 'w', encoding='utf-8') as f: | |
f.writelines(sorted_lines) | |
# 一致しない行を新しいファイルに書き出す (UTF-8 エンコーディング) | |
with open(unmatched_file, 'w', encoding='utf-8') as f: | |
f.writelines(unmatched_lines) | |
print(f"処理結果が {output_file} に書き出されました。") | |
print(f"一致しなかった行が {unmatched_file} に書き出されました。") | |
# 入力ファイルの削除 | |
os.remove(file1) | |
os.remove(file2) | |
print(f"入力ファイル {file1} と {file2} が削除されました。") | |
if __name__ == "__main__": | |
# コマンドライン引数の処理 | |
parser = argparse.ArgumentParser(description="2つのファイルを結合し、一致する行を処理し、一致しない行を別ファイルに書き出します。") | |
parser.add_argument("file1", help="処理する最初のファイル名を指定してください。") | |
parser.add_argument("file2", help="処理する2つ目のファイル名を指定してください。") | |
args = parser.parse_args() | |
# ファイルを処理して書き出す関数を呼び出す | |
process_files(args.file1, args.file2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment