亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在python中創建一個CSS文件

如何在python中創建一個CSS文件

大話西游666 2022-01-05 12:19:26
如何在python中創建一個CSS文件# -*- coding: utf-8 -*-import cssutilscss = u'''/* a comment */    .chat {        background: #fff;        color: white;    }    .chat-history {        height: 100px;        padding: 8px 24px;        overflow-y: scroll;    }#live-chat header {    background: #293239;    border-radius: 5px 5px 0 0;    color: #fff;    cursor: pointer;    padding: 16px 24px;}'''sheet = cssutils.parseString(css)for rule in sheet:    if rule.type == rule.STYLE_RULE:        # find property        for property in rule.style:            if property.name == 'color':                property.value = 'green'                property.priority = 'IMPORTANT'                break        # or simply:        rule.style['margin'] = '01.0eM' # or: ('1em', 'important')# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2print(sheet.cssText)with open("hello.css", 'w') as f:    f.write(str(sheet.cssText))當我發現我的文件時b'/* a comment */\n.chat {\n    background: #fff;\n    color: green !important;\n    margin: 1em\n    }\n.chat-history {\n    height: 100px;\n    padding: 8px 24px;\n    overflow-y: scroll;\n    margin: 1em\n    }\n#live-chat header {\n    background: #293239;\n    border-radius: 5px 5px 0 0;\n    color: green !important;\n    cursor: pointer;\n    padding: 16px 24px;\n    margin: 1em\n    }'我想創建一個縮進的 CSS 文件。
查看完整描述

1 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

您可以在寫入 CSS 文件之前cssText使用該函數解碼in ASCIIsheet.cssText.decode('ascii')


# -*- coding: utf-8 -*-

import cssutils


css = '''/* a comment */

    .chat {

        background: #fff;

        color: white;

    }


    .chat-history {

        height: 100px;

        padding: 8px 24px;

        overflow-y: scroll;

    }

#live-chat header {

    background: #293239;

    border-radius: 5px 5px 0 0;

    color: #fff;

    cursor: pointer;

    padding: 16px 24px;

}

'''

sheet = cssutils.parseString(css)


for rule in sheet:

    if rule.type == rule.STYLE_RULE:

        # find property

        for property in rule.style:

            if property.name == 'color':

                property.value = 'green'

                property.priority = 'IMPORTANT'

                break

        # or simply:

        rule.style['margin'] = '01.0eM' # or: ('1em', 'important')



# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2

cssTextDecoded = sheet.cssText.decode('ascii')

print(cssTextDecoded)

with open("hello.css", 'w') as f:

    f.write(cssTextDecoded)



查看完整回答
反對 回復 2022-01-05
  • 1 回答
  • 0 關注
  • 356 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號