SoFunction
Updated on 2024-11-07

Python's method for formatting compressed JS files

This article example describes the method of Python formatting compressed JS files. Shared for your reference. Specific analysis is as follows:

The script can be compressed js file format on some restore, of course, will not be 100% perfect, do not deal with syntax problems, just to facilitate the reading of js code

lines = open("").readlines()[0].split(";")
# Generally compressed files have all the code in one line
# Set the index as appropriate, in my case line 0 is the source code.
indent = 0
formatted = []
for line in lines:
  newline = []
  for char in line:
    (char)
    if char=='{': #{ is the basis for indentation
      indent+=1
      ("\n")
      ("\t"*indent)
    if char=="}":
      indent-=1
      ("\n")
      ("\t"*indent)
  ("\t"*indent+"".join(newline))
open("","w").writelines(";\n".join(formatted))

I hope that what I have described in this article will help you in your Python programming.