python 使用占位符值替换指定的十六进制颜色值的所有出现。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用占位符值替换指定的十六进制颜色值的所有出现。相关的知识,希望对你有一定的参考价值。

1. Preperation: Put color values in a file. One hex value per line. Case does not matter. (See old_color_example.txt for an example.)
2. Execute script python3 hex_replace.py /path/to/old_color_example.txt /path/to/YourColorScheme.thTheme
3. Result: A copy of your .tmTheme file will be generated with all the respective hex values replaced.

Modification of placeholder:
You can simply state a custom placeholder value as a third command line argument. Like so:
python3 hex_replace.py /path/to/old_color_example.txt /path/to/YourColorScheme.thTheme CUSTOM_PLACEHOLDER
#00FF00
#0AEB77
#13FFA3
#218B97
#673000
#6969FA
#7DC5F9
#AA00AA
#BBFD0D
#EDBA00
#EF9975
#F6AA11
#FEF611
#FF4028
#FF6F5B
#FFA325
#FFFEF7
import sys
import re

OLD_HEX_FILE = sys.argv[1]
THEME_FILE = sys.argv[2]
OUTPUT_FILE = "out.txt"


pat = re.compile(r"#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})")
pholder = "EEEEEE00"
if sys.argv[3]:
    pholder = sys.argv[3]

def find_all_hex(file_path):
    with open(file_path, 'r') as file:
        content = file.read()
    matches = re.findall(pat, content)
    return list(set(matches))


old_hex = find_all_hex(OLD_HEX_FILE)

pat = "|".join(old_hex)

print(pat)

with open(THEME_FILE, "r") as theme_file, open(OUTPUT_FILE, "a") as out_f:
    original = theme_file.read()
    changed = re.sub(pat, pholder, original, flags=re.IGNORECASE)
    out_f.write(changed)

以上是关于python 使用占位符值替换指定的十六进制颜色值的所有出现。的主要内容,如果未能解决你的问题,请参考以下文章

Tailwind CSS 更改占位符选项的文本颜色

用点击的 span 标签的 innerHTML 替换占位符值

文本框的占位符值不会更改为原始值?

用 pom.xml 中的值替换占位符名称

使用元素 ui 选择显示占位符值

在 iOS 中输入日期占位符值