【python前端小工具】:复制less/scss/stulys文件,将其中的rpx单位转换成vw单位

可自行修改文件路径和配置

import re
fileName = "course"
with open(f'./src/less/{fileName}.less','r') as lines:
    text ="" 
    for line in lines:
        if(line and line.find("rpx")!=-1):
           hasRpxLine = re.findall("\d*",line)
           for rpx in hasRpxLine:
               if(rpx):
                  num = int(rpx)
                  nowStr =  '{0}rpx'.format(rpx)
                  num =  round(100*num/750, 2)
                  currentStr = '{0}vw'.format(num)
                  line = line.replace(nowStr, currentStr)
        text = f'{text}{line}'
    print(text)
    with open (f'./src/less/{fileName}_copy.less','w') as file:
        file.write(text)

windows中默认的编码方式是gbk,会报错,可设置打开方式为utf-8,或者删掉中文注释

设置方式为encoding="UTF-8"

 open(f'./src/less/{fileName}.less','r',encoding="UTF-8")

你可能感兴趣的:(【python前端小工具】:复制less/scss/stulys文件,将其中的rpx单位转换成vw单位)