How to format the compressed JS file in Python

Source: Internet
Author: User
The example in this paper describes how Python formats the compressed JS file. Share to everyone for your reference. The specific analysis is as follows:

The script can be compressed after the JS file format for some restore, of course, will not be perfect, temporarily do not deal with grammar problems, just to facilitate reading JS code

Lines = open ("Unformated.js"). ReadLines () [0].split (";") #一般压缩后的文件所有代码都在一行里 # Set the index as appropriate, and in my case line No. 0 is the source code. indent = 0formatted = []for line in lines:  newline = [] for char in line  :    newline.append (char)    if char== ' { ': #{is the basis for indentation      indent+=1      newline.append ("\ n")      newline.append ("\ T" *indent)    if char== "}":      indent- =1      newline.append ("\ n")      newline.append ("\ T" *indent)  formatted.append ("\ T" *indent+ "". Join (newline ) Open ("Formated.js", "W"). Writelines ("; \ n". Join (formatted))

Hopefully this article will help you with Python programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.