簡體   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