Python格式化css文件的方法
作者:Sephiroth 时间:2021-02-14 11:38:34
本文实例讲述了Python格式化css文件的方法。分享给大家供大家参考。具体实现方法如下:
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 = string.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
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,格式化,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用requests实现发送带文件请求功能
2023-11-03 14:23:13
[译]在线广告及其在网页设计中的重要性
2009-02-06 13:36:00
![](https://img.aspxhome.com/file/UploadPic/20092/6/001-22s.jpg)
Python实现日志实时监测的示例详解
2023-01-06 15:48:29
![](https://img.aspxhome.com/file/2023/9/118339_0s.png)
Python Matplotlib实现三维数据的散点图绘制
2021-01-30 09:45:19
![](https://img.aspxhome.com/file/2023/2/87672_0s.jpg)
python中assert用法实例分析
2023-05-22 06:01:40
在tensorflow中设置使用某一块GPU、多GPU、CPU的操作
2023-07-22 11:37:28
Python函数中apply、map、applymap的区别
2022-08-11 04:20:36
Python计算多幅图像栅格值的平均值
2021-03-28 01:41:05
asp 批量删除选中的多条记录
2011-03-29 10:33:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112326166.gif)
Python的信号库Blinker用法详解
2021-06-23 23:23:19
Python下简易的单例模式详解
2022-02-15 10:03:19
Python命令行定时任务自动化工作流程
2023-01-01 20:52:51
![](https://img.aspxhome.com/file/2023/0/99690_0s.png)
Python网络编程之Python编写TCP协议程序的步骤
2022-12-25 17:18:55
![](https://img.aspxhome.com/file/2023/0/118550_0s.jpg)
python全局变量引用与修改过程解析
2021-12-13 18:55:37
![](https://img.aspxhome.com/file/2023/3/112983_0s.png)
基于Keras的格式化输出Loss实现方式
2021-10-20 20:44:00
![](https://img.aspxhome.com/file/2023/2/92312_0s.jpg)
python基础之Socket套接字详解
2023-04-11 01:52:52
![](https://img.aspxhome.com/file/2023/7/117627_0s.png)
基于Python实现文件分类器的示例代码
2023-06-02 12:49:10
![](https://img.aspxhome.com/file/2023/9/90279_0s.png)
python使用selenium实现批量文件下载
2022-09-23 15:27:08
![](https://img.aspxhome.com/file/2023/3/105053_0s.png)
基python实现多线程网页爬虫
2023-10-30 00:58:48
Python爬虫入门教程01之爬取豆瓣Top电影
2023-05-22 16:45:00
![](https://img.aspxhome.com/file/2023/1/86251_0s.png)