简体   繁体   English

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

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

I am just trying to print the Unicode number ranging from 1 to 100 in python.我只是想在 python 中打印从 1 到 100 的 Unicode 数字。 I have searched a lot in StackOverflow but no question answers my queries.我在 StackOverflow 中搜索了很多,但没有问题能回答我的疑问。

So basically I want to print Bengali numbers from ১ to ১০০.所以基本上我想打印从 ১ 到 ১০০ 的孟加拉数字。 The corresponding English number is 1 to 100.对应的英文数字是1到100。

What I have tried is to get the Unicode number of ১ which is '\১'.我所尝试的是获取 ১ 的 Unicode 编号,即“\১”。 Then I have tried to increase this number by 1 as depicted in the following code:然后我尝试将这个数字增加 1,如下面的代码所示:

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

But the above code says to me the following output.但是上面的代码告诉我以下输出。

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

So what I want is to get a number series as following:所以我想要的是得到一个数字系列如下:

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

TypeError: can only concatenate str (not "int") to str1 I wish if there is any solution to this. TypeError: can only concatenate str (not "int") to str1 如果有任何解决方案,我希望。 Thank you.谢谢。

Make a translation table.制作翻译表。 The function str.maketrans() takes a string of characters and a string of replacements and builds a translation dictionary of Unicode ordinals to Unicode ordinals.函数str.maketrans()接受一个字符串和一个替换字符串,并构建一个 Unicode 序数到 Unicode 序数的翻译字典。 Then, convert a counter variable to a string and use the translate() function on the result to convert the string:然后,将计数器变量转换为字符串,并对结果使用translate()函数来转换字符串:

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

Output:输出:

1 ১ 2 ২ 3 ৩ 4 ৪ 5 ৫ 6 ৬ 7 ৭ 8 ৮ 9 ৯ 10 ১০ 11 ১১ 12 ১২ 13 ১৩ 14 ১৪ 15 ১৫ 16 ১৬ 17 ১৭ 18 ১৮ 19 ১৯ 20 ২০ 21 ২১ 22 ২২ 23 ২৩ 24 ২৪ 25 ২৫ 26 ২৬ 27 ২৭ 28 ২৮ 29 ২৯ 30 ৩০ 31 ৩১ 32 ৩২ 33 ৩৩ 34 ৩৪ 35 ৩৫ 36 ৩৬ 37 ৩৭ 38 ৩৮ 39 ৩৯ 40 ৪০ 41 ৪১ 42 ৪২ 43 ৪৩ 44 ৪৪ 45 ৪৫ 46 ৪৬ 47 ৪৭ 48 ৪৮ 49 ৪৯ 50 ৫০ 51 ৫১ 52 ৫২ 53 ৫৩ 54 ৫৪ 55 ৫৫ 56 ৫৬ 57 ৫৭ 58 ৫৮ 59 ৫৯ 60 ৬০ 61 ৬১ 62 ৬২ 63 ৬৩ 64 ৬৪ 65 ৬৫ 66 ৬৬ 67 ৬৭ 68 ৬৮ 69 ৬৯ 70 ৭০ 71 ৭১ 72 ৭২ 73 ৭৩ 74 ৭৪ 75 ৭৫ 76 ৭৬ 77 ৭৭ 78 ৭৮ 79 ৭৯ 80 ৮০ 81 ৮১ 82 ৮২ 83 ৮৩ 84 ৮৪ 85 ৮৫ 86 ৮৬ 87 ৮৭ 88 ৮৮ 89 ৮৯ 90 ৯০ 91 ৯১ 92 ৯২ 93 ৯৩ 94 ৯৪ 95 ৯৫ 96 ৯৬ 97 ৯৭ 98 ৯৮ 99 ৯৯ 100 ১০০ 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

You can try this.你可以试试这个。 Convert the character to an integer .character转换为integer Do the addition and the convert it to character again.添加并再次将其转换为character If the number is bigger than 10 you have to convert both digits to characters that's why we are using modulo % .如果数字大于 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']))

You can try running it here https://repl.it/repls/GloomyBewitchedMultitasking您可以尝试在此处运行它https://repl.it/repls/GloomyBewitchedMultitasking

EDIT: Providing also javascript code as asked in comments.编辑:还提供评论中要求的 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