-
Notifications
You must be signed in to change notification settings - Fork 3
/
raw_string_changer.py
74 lines (61 loc) · 2.82 KB
/
raw_string_changer.py
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
68
69
70
71
72
73
74
import os
import json
def replace_patterns_and_backslashes_in_ipynb(file_path):
# Load the notebook JSON
with open(file_path, 'r', encoding='utf-8') as f:
notebook = json.load(f)
# Define the replacement function for patterns and backslashes
def replace_patterns_in_string(s):
# Replace label=' with label=r'
s = s.replace("label='", "label=r'")
# Replace label(' with label(r'
s = s.replace("label('", "label(r'")
# Replace label="" with label=r"
s = s.replace('label="', 'label=r"')
# Replace label(" with label(r"
s = s.replace('label("', 'label(r"')
# Replace description=' with description=r'
s = s.replace("description='", "description=r'")
# Replace description="" with description=r"
s = s.replace('description="', 'description=r"')
# Replace annotate(' with annotate(r'
s = s.replace("annotate('", "annotate(r'")
# Replace annotate(" with annotate(r"
s = s.replace('annotate("', 'annotate(r"')
# Replace annotate(' with annotate(r'
s = s.replace("title('", "title(r'")
# Replace annotate(" with annotate(r"
s = s.replace('title("', 'title(r"')
# Replace annotate(' with annotate(r'
s = s.replace("text('", "text(r'")
# Replace annotate(" with annotate(r"
s = s.replace('text("', 'text(r"')
# Replace double backslashes \\ with single backslash \
s = s.replace('\\\\', '\\')
return s
# Recursively apply the replacements to all cells
def transform_cell(cell):
if isinstance(cell, str):
# Apply the string replacements to each string
return replace_patterns_in_string(cell)
elif isinstance(cell, list):
return [transform_cell(item) for item in cell]
elif isinstance(cell, dict):
return {key: transform_cell(value) for key, value in cell.items()}
else:
return cell
# Apply the transformation to the entire notebook
notebook = transform_cell(notebook)
# Save the modified notebook back
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(notebook, f, ensure_ascii=False, indent=2)
print(f"All replacements have been made successfully in {file_path}.")
# Function to apply the script to all .ipynb files in the current folder
def process_all_ipynb_files_in_folder(folder_path):
for file_name in os.listdir(folder_path):
if file_name.endswith('.ipynb'):
file_path = os.path.join(folder_path, file_name)
replace_patterns_and_backslashes_in_ipynb(file_path)
# Run the script for all .ipynb files in the current directory
#folder_path = '.' # You can specify another folder path if needed
#process_all_ipynb_files_in_folder(folder_path)