unicon转utf-8脚本

import re
import sys

if len(sys.argv) < 2:
    print("请传入要打开的文件名作为参数")
else:
    file_name = sys.argv[1]
# 读取文件内容
    with open(file_name, 'r', encoding='utf-8') as file1:
        content = file1.read()
        with open('output.txt', 'w') as file2:
            pattern = r'&#x([\da-fA-F]{4});'
            matches = re.findall(pattern, content)
            lines = content.split('\n')
            i = 0
            for line in lines:
                if re.match(r'^[a-zA-Z]', line.strip()):
                    print("#define LV_", end='')
                    print(line.strip().upper(), end='')
                    file2.write(line.strip().upper())
                    unicode_char = chr(int(matches[i], 16))
                    utf8_bytes = unicode_char.encode('utf-8')
                    # 将 UTF-8 编码转换为十六进制表示
                    utf8_hex = ' '.join(['{:02X}'.format(b) for b in utf8_bytes])
                    file2.write("     \"")
                    print("     \"", end='')
                    for j in range(0, len(utf8_hex), 3):
                        dat1 = utf8_hex[j:j + 2]
                        if len(dat1) == 2:  # 确保取出的单位长度为2
                            file2.write("\\x")
                            print("\\x", end='')
                            file2.write(dat1)
                            print(dat1, end='')
                    file2.write("\"\n")
                    print("\"")
                    i += 1

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值