Python script implements format CSS file _python

Source: Internet
Author: User
Tags python script

Recently research on CSS, not to go online analysis of other people's Web pages, but many of the Web site's CSS files are either written in one line, or a line is not, it seems extremely painful, so write a script to convert to a more readable format. Here's the script:

Import string, sys
import Re, Stringio

tab=4


def format (ss):
  f = open (ss, "R")
  data = F.read ()
  F . Close ()
  
  Dlen = len (data)
  i = 0
  buf = Stringio.stringio ()
  start = 0 while
  i < dlen:
    if data[ I] = = = = ' {':
      
      buf.write (data[start:i] + ' {')
      i = i + 1
      start = i
    elif data[i] = = = ': Last
      = Strin G.strip (data[start:i])
      if last:
        buf.write (' *tab + last + '; ')
      Buf.write ('} ')
      i = i + 1
      start = i
      
    elif data[i] = = '; ': line
      = String.strip (data[start:i))
      
      buf. Write (' *tab + line + '; '
      i = i + 1
      start = i
    
    else:
      i = i + 1
  buf.write (data[start:i+1]) return
  
  buf.getvalue () 
   if __name__ = = ' __main__ ':
  If Len (sys.argv) = = 1:
    print ' usage:cssformat.py filename '
    sys.exit ()
  
  ret = format (sys.argv[1])
  PRINT ret

How to use:

Python cssformat.py  file names to be converted > saved files after conversion

Related Article

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.