繁体   English   中英

如何在python中打印unicode数字系列?

[英]how to print unicode number series in python?

我只是想在 python 中打印从 1 到 100 的 Unicode 数字。 我在 StackOverflow 中搜索了很多,但没有问题能回答我的疑问。

所以基本上我想打印从 ১ 到 ১০০ 的孟加拉数字。 对应的英文数字是1到100。

我所尝试的是获取 ১ 的 Unicode 编号,即“\১”。 然后我尝试将这个数字增加 1,如下面的代码所示:

x = '\u09E7'
print(x+1)

但是上面的代码告诉我以下输出。

TypeError: can only concatenate str (not "int") to str

所以我想要的是得到一个数字系列如下:

১, ২, ৩, ৪, ৫, ৬, ৭, ৮, ৯, ১০, ১, ১২, ১৩, ............, ১০০

TypeError: can only concatenate str (not "int") to str1 如果有任何解决方案,我希望。 谢谢。

制作翻译表。 函数str.maketrans()接受一个字符串和一个替换字符串,并构建一个 Unicode 序数到 Unicode 序数的翻译字典。 然后,将计数器变量转换为字符串,并对结果使用translate()函数来转换字符串:

#coding:utf8
xlat = str.maketrans('0123456789','০১২৩৪৫৬৭৮৯')
for i in range(1,101):
    print(f'{i:3d} {str(i).translate(xlat)}',end=' ')

输出:

1 1 2২3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 121২13 13 14 14 15 15 16 16 17 17 18 18 19 19 20২021২122২২23২324২425২5 26২627২728২829২930 30 31 31 323২33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 424২43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 525২53 53 54 54 55 55 56 56 57 57 58 58 59 59 60 60 61 61 626২63 63 64 64 65 65 66 66 67 67 68 68 69 69 70 70 71 71 727২73 73 74 74 75 75 76 76 77 77 78 78 79 79 80 80 81 81 828২83 83 84 84 85 85 86 86 87 87 88 88 89 89 90 90 91 91 929২93 93 94 94 95 95 96 96 97 97 98 98 99 99 100 100

你可以试试这个。 character转换为integer 添加并再次将其转换为character 如果数字大于 10,则必须将两个数字都转换为字符,这就是我们使用 modulo %的原因。

if num < 10:
   x = ord('\u09E6')
   print(chr(x+num))
elif num < 100:
   mod = num % 10
   num = int((num -mod) / 10)
   x = ord('\u09E6')
   print(''.join([chr(x+num), chr(x+mod)]))
else:
   x = ord('\u09E6')
   print(''.join([chr(x+1), '\u09E6', '\u09E6']))

您可以尝试在此处运行它https://repl.it/repls/GloomyBewitchedMultitasking

编辑:还提供评论中要求的 javascript 代码。

function getAsciiNum(num){
    zero = "০".charCodeAt(0)
    if (num < 10){
        return(String.fromCharCode(zero+num))
    }
    else if (num < 100) {
      mod = num % 10
      num = Math.floor((num -mod) / 10)
      return(String.fromCharCode(zero+num) + String.fromCharCode(zero+mod))
    }
    else {
      return(String.fromCharCode(zero+1) + "০০")
    }
}

console.log(getAsciiNum(88))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM