I am reading a bunch of js files using Python. I am finding out the tags: section and updating it with additional values. My script is changing the previous indentation of the tags, especially for the closing bracket ].
Here is what my loop looks like:
def update_js_file(filename: str, tags: list) -> None:
content = ''
with open(filename, 'r', encoding='utf-8') as f:
content = f.read()
tags_raw = json.dumps(tags, indent=4)
content = re.sub(r'tags: \[.+?\]', 'tags: ' + tags_raw, content, flags=re.DOTALL)
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
Git diff shows the following change in file:
tags: [
- "aws",
- "cloudtrail",
- "cap-04",
- "mon-08"
- ],
+ "aws",
+ "cloudtrail",
+ "cap-04",
+ "mon-08",
+ "NIST-800-171",
+ "NIST-800-171-3.3.8"
+],
How can I avoid this change in indentation for each tag and the closing bracket.
Edit: I was able to fix the indentation by changing the value of indentation to 8 in tags_raw but I am still not able to properly indent the closing bracket ].