-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathremove_selected_char.py
More file actions
67 lines (54 loc) · 2.34 KB
/
remove_selected_char.py
File metadata and controls
67 lines (54 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# encoding=utf-8
import argparse
def process_sets(args):
try:
# 讀取完整檔案內容並轉為集合
with open(args.input, encoding='utf-8') as f:
input_set = set(f.read().strip())
with open(args.remove, encoding='utf-8') as f:
remove_set = set(f.read().strip())
# 定義運算模式對應表
operations = {
"subtract": input_set - remove_set,
"sub": input_set - remove_set,
"-": input_set - remove_set,
"union": input_set | remove_set,
"add": input_set | remove_set,
"+": input_set | remove_set,
"intersect": input_set & remove_set,
"int": input_set & remove_set,
"&": input_set & remove_set
}
if args.mode not in operations:
print(f"Error: Invalid mode '{args.mode}'.")
return
target_set = operations[args.mode]
# 排序並格式化結果
sorted_chars = sorted(target_set)
formatted_result = ''.join(sorted_chars)
# 輸出統計資訊
print(f"length of input file: {len(input_set)}")
print(f"length of remove file: {len(remove_set)}")
print(f"final length of target file: {len(target_set)}")
# 寫入檔案
if formatted_result:
with open(args.output, 'w', encoding='utf-8') as outfile:
outfile.write(formatted_result)
else:
print("Warning: Target set is empty. No file was written.")
except FileNotFoundError:
print("Error: Input or remove file not found.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def cli():
parser = argparse.ArgumentParser(description="process char sets")
parser.add_argument("--input", "-i", help="input text file", required=True, type=str)
parser.add_argument("--remove", "-r", help="string to process", required=True, type=str)
parser.add_argument("--output", "-o", help="output text file", default="new.txt", type=str)
parser.add_argument("--mode", "-m", help="mode of operation", default="subtract",
choices=["subtract", "sub", "-", "union", "add", "+", "intersect", "int", "&"], type=str)
args = parser.parse_args()
process_sets(args)
if __name__ == "__main__":
cli()