Commit
•
a4f8599
1
Parent(s):
4d8987c
Upload cswm.py
Browse files
cswm.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import shutil
|
3 |
+
import re
|
4 |
+
|
5 |
+
def replace_characters_between_strings(filename, copyname, stringA, stringB, replacements, encoding='utf-8'):
|
6 |
+
# Create a copy of the original file
|
7 |
+
shutil.copyfile(filename, copyname)
|
8 |
+
|
9 |
+
# Open the original file for reading with the specified encoding
|
10 |
+
with open(filename, 'r', encoding=encoding) as input_file:
|
11 |
+
# Open the copy for writing with the specified encoding
|
12 |
+
with open(copyname, 'w', encoding=encoding) as output_file:
|
13 |
+
inside_text = False
|
14 |
+
|
15 |
+
# Process each line in the input file
|
16 |
+
for line in input_file:
|
17 |
+
if re.search(re.escape(stringA), line):
|
18 |
+
inside_text = True
|
19 |
+
|
20 |
+
# Replace the characters in the line if inside_text is True
|
21 |
+
if inside_text:
|
22 |
+
for char_to_replace, replacement_char in replacements:
|
23 |
+
line = line.replace(char_to_replace, replacement_char)
|
24 |
+
|
25 |
+
output_file.write(line)
|
26 |
+
|
27 |
+
if re.search(re.escape(stringB), line):
|
28 |
+
inside_text = False
|
29 |
+
|
30 |
+
# Parse command-line arguments
|
31 |
+
parser = argparse.ArgumentParser(description='Replace user-defined characters between user-defined strings in a file.')
|
32 |
+
parser.add_argument('filename', type=str, nargs='?', help='the name of the input file')
|
33 |
+
parser.add_argument('copyname', type=str, nargs='?', help='the name of the copy file')
|
34 |
+
parser.add_argument('stringA', type=str, nargs='?', help='the starting string')
|
35 |
+
parser.add_argument('stringB', type=str, nargs='?', help='the ending string')
|
36 |
+
parser.add_argument('replacements', nargs='*', metavar=('char_to_replace', 'replacement_char'), help='character replacements (e.g., a b c d)')
|
37 |
+
parser.add_argument('--encoding', type=str, default='utf-8', help='the file encoding (default: utf-8)')
|
38 |
+
args = parser.parse_args()
|
39 |
+
|
40 |
+
# Prompt for missing arguments if not provided
|
41 |
+
if not args.filename:
|
42 |
+
args.filename = input("Enter the name of the input file: ")
|
43 |
+
if not args.copyname:
|
44 |
+
args.copyname = input("Enter the name of the output file: ")
|
45 |
+
if not args.stringA:
|
46 |
+
args.stringA = input("Enter the starting string: ")
|
47 |
+
if not args.stringB:
|
48 |
+
args.stringB = input("Enter the ending string: ")
|
49 |
+
if not args.replacements:
|
50 |
+
args.replacements = []
|
51 |
+
while True:
|
52 |
+
char_to_replace = input("Enter the character to replace (or press Enter to finish): ")
|
53 |
+
if not char_to_replace:
|
54 |
+
break
|
55 |
+
replacement_char = input("Enter the replacement character: ")
|
56 |
+
args.replacements.append((char_to_replace, replacement_char))
|
57 |
+
|
58 |
+
# Execute the program with the provided arguments
|
59 |
+
replace_characters_between_strings(args.filename, args.copyname, args.stringA, args.stringB, args.replacements, encoding=args.encoding)
|