有些网页的css文件格式很混乱,都写成1行了,如:
为此,我们写了一个python3脚本,用语格式化CSS文件,格式化后的效果如图:
#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@project:normal_css
@author:xiangguosun
@contact:[email protected]
@website:http://blog.csdn.net/github_36326955
@file: normal.py
@platform: macOS High Sierra 10.13.1 Pycharm pro 2017.1
@time: 2018/01/28
"""
def normal(file_path, out):
with open(file_path, 'r') as file:
data = file.read()
outstring = data.replace("{", "{\n").replace("}", "\n}\n\n").replace(";", ";\n")
with open(out, 'w') as outfile:
outfile.write(outstring)
print("done!")
if __name__ == "__main__":
normal('origin.css', 'normal.css')
如果你是在webstorm下进行编辑的话,还可以进一步安快捷键command+option+L(mac os)来进一步调整缩进, 不过我觉得已经没必要了。