一个账号 发表于 2020-4-7 11:20:28

Python Unicode 转 汉字

本帖最后由 一个账号 于 2020-4-10 22:11 编辑

def convert(content):
    return content.encode("UTF-8").decode("Unicode_Escape")

if __name__ == "__main__":
    while True:
      content = input("请输入 Unicode 代码:")
      
      target = convert(content)
      
      print(f"\n{target}\n")

from requests import post
from bs4 import BeautifulSoup as bs


def convert(content):
    def find_data(res):
      soup = bs(res.text, "html.parser")

      target = soup.find("textarea", id="result")

      return target.text

    data = {}
    data["content"] = content
    data["untoch"] = "Unicode 转 中文"
   
    res = post("http://tool.chinaz.com/tools/unicode.aspx", data=data)
    target = find_data(res)

    return target

if __name__ == "__main__":
    while True:
      content = input("请输入 Unicode 代码:")
      
      target = convert(content)
      
      print(f"\n{target}\n")

请输入 Unicode 代码:\u5317\u4eac\u5e02

北京市

永恒的蓝色梦想 发表于 2020-4-7 11:27:06

本帖最后由 永恒的蓝色梦想 于 2020-4-8 21:25 编辑

while (temp:=input("请输入 Unicode 代码:")):
    print(chr(int(temp,base=16)))

一个账号 发表于 2020-4-10 21:22:46

永恒的蓝色梦想 发表于 2020-4-7 11:27


转换的是 16 进制的 Unicode

永恒的蓝色梦想 发表于 2020-4-10 21:24:38

一个账号 发表于 2020-4-10 21:22
转换的是 16 进制的 Unicode

给个输入输出示例

一个账号 发表于 2020-4-10 22:10:41

永恒的蓝色梦想 发表于 2020-4-10 21:24
给个输入输出示例

OK
页: [1]
查看完整版本: Python Unicode 转 汉字