如何在 Python 中制作经过验证的符号?

问题描述

我正在开发一个使用 Python 下载和安装应用程序的项目。存储应用程序信息的存储库在 json 中有一行说明用户是否已通过验证,然后将其转换为 TrueFalse 变量。下载用户信息后,它使用 rich.print 函数在屏幕上打印信息,在 rich 模块中。我希望能够打印经过验证的符号,例如在 Twitter 上,我该怎么做?

manifest.json

{
  "Name": "Test package","License": "Public Domain","Developer": "Hearth OS","DeveloperWWW": "https://hearth-os.github.io/","Description": "Hello World package.","Verified": "True","Version": "0.1","AppID": "com.hearth-os.test"
}
main.py (Lines 72-90)

# Write and open manifest.json
open("manifest.json",'wb').write(r.content)
jsonFile = open("manifest.json","r")
manifest = json.load(jsonFile)

# Read `manifest`
name = manifest['Name']
license = manifest['License']
developer = manifest['Developer']
www = manifest['DeveloperWWW']
description = manifest['Description']
verified = manifest['Verified']
version = manifest['Version']
id = manifest['AppID']

# Print `manifest`
rich.print('Package: ' + name)
rich.print('Developer: [link=' + www + ']' + developer + '[/link]')
rich.print('License: ' + license)

解决方法

如果您的“验证符号”是一个 unicode 字符,您可以直接打印它:

if verified:
    print("✅")

如果在 python2 中,您可能需要在文件顶部指定编码,否则会出现错误。 UTF-8 是一个常见的选择(也是 python3 中的默认值)

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

您还可以通过使用 unicode 转义序列来避免在代码中嵌入 unicode 文字字符(您需要查找所选符号的代码):

print("\u2705")