import re
def px_2_em(matched):
value = matched.group(0)
n= int(value[0:-2]) / 16
return str(n) + 'em'
# replace: {n}px / 16 -> {x}em
def replace(string):
regexp = r'[0-9]+px'
result = re.sub( regexp, px_2_em , string)
return result
def start(originFile, saveFle):
with open(originFile, 'r') as f:
# get string
string = f.read()
# replace
res = replace(string)
# write
newFile = open(saveFle, "w+")
newFile.write(res)
newFile.close()
# close file
f.close()
start('./table.html','./table_em.html')
python: 把 html中的 “40px” 转换为 “2.5em”
于 2021-11-25 14:56:24 首次发布
该代码段展示了一个Python函数,将CSS中的像素(px)单位转换为相对单位em。通过正则表达式匹配px值,并进行换算。整个过程包括读取文件,替换字符串中的px单位为em,然后将结果写入新的文件中,实现静态资源的尺寸适配。
摘要由CSDN通过智能技术生成