在Python中将文本转换为盲文(Unicode)

2024-05-12 13:03:18 发布

您现在位置:Python中文网/ 问答频道 /正文

根据Wikipedia,盲文的Unicode块是U+2800。。U+28FF。在

我正在尝试将普通文本转换为盲文符号(点)。为此,我要映射这个字符串:

" A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="

映射这个特定字符串的原因被提到on this Wikipedia page

我的代码:

^{pr2}$

我想打印像这样的盲文字符

input = hello world
output = ⠓⠑⠇⠇⠕ ⠺⠕⠗⠇⠙

问题是我的输出看起来像这样

Input = A
Output = 2801

Tags: 字符串文本unicode符号原因wikipediacifmsp
2条回答

我使用了一个简单的Python字典,它可以工作,但它非常基础

# ASCII
asciicodes = [' ','!','"','#','$','%','&','','(',')','*','+',',','-','.','/',
          '0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@',
          'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
          'r','s','t','u','v','w','x','y','z','[','\\',']','^','_']

# Braille symbols
brailles = ['⠀','⠮','⠐','⠼','⠫','⠩','⠯','⠄','⠷','⠾','⠡','⠬','⠠','⠤','⠨','⠌','⠴','⠂','⠆','⠒','⠲','⠢',
        '⠖','⠶','⠦','⠔','⠱','⠰','⠣','⠿','⠜','⠹','⠈','⠁','⠃','⠉','⠙','⠑','⠋','⠛','⠓','⠊','⠚','⠅',
        '⠇','⠍','⠝','⠕','⠏','⠟','⠗','⠎','⠞','⠥','⠧','⠺','⠭','⠽','⠵','⠪','⠳','⠻','⠘','⠸']

python内置了重新映射字符串:使用^{}^{}可以执行以下操作:

intab = "helo"  # ...add the full alphabet and other characters
outtab = "⠓⠑⠇⠕" # and the characters you want them translated to
transtab = str.maketrans(intab, outtab)

strg = "hello"
print(strg.translate(transtab)) # ⠓⠑⠇⠇⠕

请注意,intab的长度必须与{}的长度匹配,如果只向maketrans传递2个参数(可以传递第三个参数;请参阅doc)。在

相关问题 更多 >