[英]How do i work on Checksum of Singapore Car License Plate with Python
I have researched and searched internet on the checksum of Singapore Car License Plate.我已经研究并搜索了新加坡车牌校验和的互联网。 For the license plate of SBA 1234, I need to convert all the digits excluding the S to numbers.
对于 SBA 1234 的车牌,我需要将除 S 之外的所有数字转换为数字。 A being 1, B being 2, and so on.
A 为 1,B 为 2,以此类推。 SBA 1234 is in a string in a text format.
SBA 1234 是文本格式的字符串。 How do i convert B and A to numbers for the calculation for the checksum while making sure that the value B and A do not change.
如何将 B 和 A 转换为用于计算校验和的数字,同时确保值 B 和 A 不变。 The conversion of B and A to numbers is only for the calculation.
将 B 和 A 转换为数字仅用于计算。 How do i do the conversion for this with Python.
我如何使用 Python 进行转换。 Please help out.
请帮忙。 Thank you.
谢谢你。
There are multiple ways to create a dictionary with values A thru Z representing values 1 thru 26. One of the simple way to do it will be:有多种方法可以创建一个字典,其值 A 到 Z 表示值 1 到 26。其中一种简单的方法是:
value = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(1,27)))
An alternate way to it would be using the ord() function.另一种方法是使用 ord() function。
ord('A') is 65. You can create a dictionary with values A thru Z representing values 1 thru 26. To do that, you can use simple code like this. ord('A') 是 65。您可以创建一个字典,其值 A 到 Z 表示值 1 到 26。为此,您可以使用这样的简单代码。
atoz = {chr(i): i - 64 for i in range(ord("A"), ord("A") + 26)}
This will provide an output with a dictionary这将为 output 提供字典
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
You can search for the char in the dictionary to get 1 thru 26.您可以在字典中搜索 char 以获得 1 到 26。
Alternate, you can directly use ord(x) - 64 to get a value of the alphabet.或者,您可以直接使用 ord(x) - 64 来获取字母表的值。 If x is A, you will get 1. Similarly, if x is Z, the value will be 26.
如果 x 为 A,您将得到 1。同样,如果 x 为 Z,则值为 26。
So you can write the code directly to calculate the value of the Singapore Number Plate as:所以你可以直接写代码来计算新加坡车牌的价值为:
snp = 'SBA 1234'
then you can get a value of那么你可以得到一个值
snp_num = [ord(snp[1]) - 64,ord(snp[2]) - 64, int(snp[4]), int(snp[5]), int(snp[6])] snp_num = [ord(snp[1]) - 64,ord(snp[2]) - 64, int(snp[4]), int(snp[5]), int(snp[6])]
This will result in这将导致
[2, 1, 1, 2, 3]
I hope one of these options will work for you.我希望这些选项之一对您有用。 Then use the checksum function to do your calculation.
然后使用校验和 function 进行计算。 I hope this is what you are looking for.
我希望这就是你要找的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.