The example in this article describes the way Python formats CSS files. Share to everyone for your reference. The implementation methods are as follows:
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
I hope this article will help you with your Python programming.