Python应用—获得汉字的unicode编码

1.需求

在编程中有时需要获得汉字的unicode 16进制编码,比如正则表达式。该小程序可以输入汉字后返回其Unicode编码,十分方便。

2.代码实现

复制代码
import PySimpleGUI as sg

# 定义一个函数来获取并显示Unicode编码
def show_unicode(input_text):
    # 获取输入框中的内容
    input_text = input_text
    # 遍历字符串中的每个字符,并转换为十六进制的Unicode编码
    unicode_hex = '\n'.join([f"字符: {char}, Unicode编码(十六进制): {ord(char):X}" for char in input_text])
    # 显示每个字符的Unicode编码
    sg.popup("Unicode编码(十六进制)", unicode_hex)

layout = [
    [sg.Text('请输入中文字符:')],
    [sg.Input(key='input_text', size=(20, 1))],
    [sg.Button('显示Unicode编码'), sg.Button('退出')]
]

# 创建窗口
window = sg.Window('Unicode编码显示', layout)

# 事件循环
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == '退出':
        break
    if event == '显示Unicode编码':
        input_text = values['input_text']
        show_unicode(input_text)

# 关闭窗口
window.close()
相关推荐
踏着七彩祥云的小丑2 小时前
pytest——Mark标记
开发语言·python·pytest
Dream of maid2 小时前
Python12(网络编程)
开发语言·网络·php
W23035765732 小时前
经典算法:最长上升子序列(LIS)深度解析 C++ 实现
开发语言·c++·算法
Y4090012 小时前
【多线程】线程安全(1)
java·开发语言·jvm
不爱吃炸鸡柳3 小时前
Python入门第一课:零基础认识Python + 环境搭建 + 基础语法精讲
开发语言·python
minji...3 小时前
Linux 线程同步与互斥(三) 生产者消费者模型,基于阻塞队列的生产者消费者模型的代码实现
linux·运维·服务器·开发语言·网络·c++·算法
Dxy12393102163 小时前
Python基于BERT的上下文纠错详解
开发语言·python·bert
SiYuanFeng4 小时前
Colab复现 NanoChat:从 Tokenizer(CPU)、Base Train(CPU) 到 SFT(GPU) 的完整踩坑实录
python·colab
wjs20245 小时前
JavaScript 语句
开发语言