【问题标题】:Python: Are there any libraries with all the unicode characters similar to the string library for ascii characters? [closed]Python:是否有任何包含所有 unicode 字符的库,类似于用于 ascii 字符的字符串库? [关闭]
【发布时间】:2020-08-11 16:32:27
【问题描述】:

在 python 中,字符串库有类似 string.ascii_letters 的方法。 Unicode 字符或符号有什么相似之处吗?我自己也找不到任何东西。

感谢您的帮助!对这种类型的东西相当新,如果这个问题很愚蠢,请道歉。

【问题讨论】:

  • 使用the unicodedata module,您可以遍历所有可能的 unicode 代码点并生成您自己的列表。
  • 请为您的问题提供代表代码和数据。

标签: python unicode python-unicode


【解决方案1】:

您可以简单地使用chr 内置函数。

返回表示其 Unicode 代码点为整数 i 的字符的字符串。例如,chr(97) 返回字符串 'a',而 chr(8364) 返回字符串 '€'。参数的有效范围是从 0 到 1,114,111(基数为 16 的 0x10FFFF)。如果 i 超出该范围,将引发 ValueError。

所以你可以做这样的事情来获得类似于string.ascii_letters的结果:

''.join(map(chr, range(1114112)))

相反,如果您只想要一个列表:

list(map(chr, range(1114112)))

【讨论】:

  • 这很有帮助,谢谢!
  • 并非该范围内的所有内容都是有效的 Unicode 字符。
  • 或“信”,但您可以在unicodedata.category()上过滤列表
猜你喜欢
  • 2015-04-22
  • 2011-01-27
  • 1970-01-01
  • 2011-03-18
  • 2011-11-20
  • 2013-09-23
  • 2017-08-27
  • 1970-01-01
  • 2018-08-10
相关资源
最近更新 更多