【发布时间】:2017-01-29 15:38:01
【问题描述】:
根据Wikipedia,盲文的Unicode块是U+2800 .. U+28FF。
我正在尝试将普通文本转换为盲文符号(点)。为此,我正在映射这个字符串:
" A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="
提到了映射这个特定字符串的原因on this Wikipedia page
我的代码:
def toBraille(c):
unic=2800
mapping = " A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)="
i = mapping.index(c.upper())
if (i>0):
unic+=i
unichex = hex(unic)
return unichr(unichex))
if (i==0):
return '_'
if (i<O):
return '?'
def converter(txt):
tmp=""
for x in txt:
tmp+=str(toBraille(x))
return tmp
txt = raw_input("Please insert text: \n")
print(converter(txt))
我想打印这样的盲文字符
input = hello world
output = ⠓⠑⠇⠇⠕ ⠺⠕⠗⠇⠙
问题是我的输出看起来像这样
Input = A
Output = 2801
【问题讨论】:
-
为什么要映射这个字符串?什么是特别的 w.r.t.你想要的转换?你希望你的普通同事理解它吗?
-
我在维基百科上读过 [en.wikipedia.org/wiki/Braille_ASCII]: 这个 C 字符串(也可以在 Python 和其他接受 C 字符串文字的编程语言中使用)为 Unicode 盲文字符提供盲文 ASCII 映射从 U+2800 到 U+283F 的顺序,从字符串开头的 U+2800 开始:
-
这是一种相当违反直觉的编码。破坏您的代码的一件事是 2800。它应该是十六进制的 0x2800。为什么不摆脱困惑,写一本普通的 Python 字典呢?
{'A':u'⠁',...}? -
只需创建一个具有所需映射的字典并通过它运行每个字符。
-
@RadLexus 完成。
标签: python