-2

我正在开发一个使用 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)
4

1 回答 1

1

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

if verified:
    print("✅")

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

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

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

print("\u2705")
于 2021-05-19T17:27:37.513 回答